Home | History | Annotate | Download | only in share
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <stdlib.h>
     18 #include "../include/utf16char.h"
     19 
     20 namespace ime_pinyin {
     21 
     22 #ifdef __cplusplus
     23 extern "C" {
     24 #endif
     25 
     26   char16* utf16_strtok(char16 *utf16_str, size_t *token_size,
     27                        char16 **utf16_str_next) {
     28     if (NULL == utf16_str || NULL == token_size || NULL == utf16_str_next) {
     29       return NULL;
     30     }
     31 
     32     // Skip the splitters
     33     size_t pos = 0;
     34     while ((char16)' ' == utf16_str[pos] || (char16)'\n' == utf16_str[pos]
     35            || (char16)'\t' == utf16_str[pos])
     36       pos++;
     37 
     38     utf16_str += pos;
     39     pos = 0;
     40 
     41     while ((char16)'\0' != utf16_str[pos] && (char16)' ' != utf16_str[pos]
     42            && (char16)'\n' != utf16_str[pos]
     43            && (char16)'\t' != utf16_str[pos]) {
     44       pos++;
     45     }
     46 
     47     char16 *ret_val = utf16_str;
     48     if ((char16)'\0' == utf16_str[pos]) {
     49       *utf16_str_next = NULL;
     50       if (0 == pos)
     51         return NULL;
     52     } else {
     53       *utf16_str_next = utf16_str + pos + 1;
     54     }
     55 
     56     utf16_str[pos] = (char16)'\0';
     57     *token_size = pos;
     58 
     59     return ret_val;
     60   }
     61 
     62   int utf16_atoi(const char16 *utf16_str) {
     63     if (NULL == utf16_str)
     64       return 0;
     65 
     66     int value = 0;
     67     int sign = 1;
     68     size_t pos = 0;
     69 
     70     if ((char16)'-' == utf16_str[pos]) {
     71       sign = -1;
     72       pos++;
     73     }
     74 
     75     while ((char16)'0' <=  utf16_str[pos] &&
     76            (char16)'9' >= utf16_str[pos]) {
     77       value = value * 10 + static_cast<int>(utf16_str[pos] - (char16)'0');
     78       pos++;
     79     }
     80 
     81     return value*sign;
     82   }
     83 
     84   float utf16_atof(const char16 *utf16_str) {
     85     // A temporary implemetation.
     86     char char8[256];
     87     if (utf16_strlen(utf16_str) >= 256) return 0;
     88 
     89     utf16_strcpy_tochar(char8, utf16_str);
     90     return atof(char8);
     91   }
     92 
     93   size_t utf16_strlen(const char16 *utf16_str) {
     94     if (NULL == utf16_str)
     95       return 0;
     96 
     97     size_t size = 0;
     98     while ((char16)'\0' != utf16_str[size])
     99       size++;
    100     return size;
    101   }
    102 
    103   int utf16_strcmp(const char16* str1, const char16* str2) {
    104     size_t pos = 0;
    105     while (str1[pos] == str2[pos] && (char16)'\0' != str1[pos])
    106       pos++;
    107 
    108     return static_cast<int>(str1[pos]) - static_cast<int>(str2[pos]);
    109   }
    110 
    111   int utf16_strncmp(const char16 *str1, const char16 *str2, size_t size) {
    112     size_t pos = 0;
    113     while (pos < size && str1[pos] == str2[pos] && (char16)'\0' != str1[pos])
    114       pos++;
    115 
    116     if (pos == size)
    117       return 0;
    118 
    119     return static_cast<int>(str1[pos]) - static_cast<int>(str2[pos]);
    120   }
    121 
    122   // we do not consider overlapping
    123   char16* utf16_strcpy(char16 *dst, const char16 *src) {
    124     if (NULL == src || NULL == dst)
    125       return NULL;
    126 
    127     char16* cp = dst;
    128 
    129     while ((char16)'\0' != *src) {
    130       *cp = *src;
    131       cp++;
    132       src++;
    133     }
    134 
    135     *cp = *src;
    136 
    137     return dst;
    138   }
    139 
    140   char16* utf16_strncpy(char16 *dst, const char16 *src, size_t size) {
    141     if (NULL == src || NULL == dst || 0 == size)
    142       return NULL;
    143 
    144     if (src == dst)
    145       return dst;
    146 
    147     char16* cp = dst;
    148 
    149     if (dst < src || (dst > src && dst >= src + size)) {
    150       while (size-- && (*cp++ = *src++))
    151         ;
    152     } else {
    153       cp += size - 1;
    154       src += size - 1;
    155       while (size-- && (*cp-- == *src--))
    156         ;
    157     }
    158     return dst;
    159   }
    160 
    161   // We do not handle complicated cases like overlapping, because in this
    162   // codebase, it is not necessary.
    163   char* utf16_strcpy_tochar(char *dst, const char16 *src) {
    164     if (NULL == src || NULL == dst)
    165       return NULL;
    166 
    167     char* cp = dst;
    168 
    169     while ((char16)'\0' != *src) {
    170       *cp = static_cast<char>(*src);
    171       cp++;
    172       src++;
    173     }
    174     *cp = *src;
    175 
    176     return dst;
    177   }
    178 
    179 #ifdef __cplusplus
    180 }
    181 #endif
    182 }  // namespace ime_pinyin
    183