aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/pinyin/3rdparty/pinyin/share/utf16char.cpp
blob: fadb6cf2f4dc12a3ee6cbb10e0c7de5e06b1c5fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include "../include/utf16char.h"

namespace ime_pinyin {

#ifdef __cplusplus
extern "C" {
#endif

  char16* utf16_strtok(char16 *utf16_str, size_t *token_size,
                       char16 **utf16_str_next) {
    if (NULL == utf16_str || NULL == token_size || NULL == utf16_str_next) {
      return NULL;
    }

    // Skip the splitters
    size_t pos = 0;
    while ((char16)' ' == utf16_str[pos] || (char16)'\n' == utf16_str[pos]
           || (char16)'\t' == utf16_str[pos])
      pos++;

    utf16_str += pos;
    pos = 0;

    while ((char16)'\0' != utf16_str[pos] && (char16)' ' != utf16_str[pos]
           && (char16)'\n' != utf16_str[pos]
           && (char16)'\t' != utf16_str[pos]) {
      pos++;
    }

    char16 *ret_val = utf16_str;
    if ((char16)'\0' == utf16_str[pos]) {
      *utf16_str_next = NULL;
      if (0 == pos)
        return NULL;
    } else {
      *utf16_str_next = utf16_str + pos + 1;
    }

    utf16_str[pos] = (char16)'\0';
    *token_size = pos;

    return ret_val;
  }

  int utf16_atoi(const char16 *utf16_str) {
    if (NULL == utf16_str)
      return 0;

    int value = 0;
    int sign = 1;
    size_t pos = 0;

    if ((char16)'-' == utf16_str[pos]) {
      sign = -1;
      pos++;
    }

    while ((char16)'0' <=  utf16_str[pos] &&
           (char16)'9' >= utf16_str[pos]) {
      value = value * 10 + static_cast<int>(utf16_str[pos] - (char16)'0');
      pos++;
    }

    return value*sign;
  }

  float utf16_atof(const char16 *utf16_str) {
    // A temporary implemetation.
    char char8[256];
    if (utf16_strlen(utf16_str) >= 256) return 0;

    utf16_strcpy_tochar(char8, utf16_str);
    return atof(char8);
  }

  size_t utf16_strlen(const char16 *utf16_str) {
    if (NULL == utf16_str)
      return 0;

    size_t size = 0;
    while ((char16)'\0' != utf16_str[size])
      size++;
    return size;
  }

  int utf16_strcmp(const char16* str1, const char16* str2) {
    size_t pos = 0;
    while (str1[pos] == str2[pos] && (char16)'\0' != str1[pos])
      pos++;

    return static_cast<int>(str1[pos]) - static_cast<int>(str2[pos]);
  }

  int utf16_strncmp(const char16 *str1, const char16 *str2, size_t size) {
    size_t pos = 0;
    while (pos < size && str1[pos] == str2[pos] && (char16)'\0' != str1[pos])
      pos++;

    if (pos == size)
      return 0;

    return static_cast<int>(str1[pos]) - static_cast<int>(str2[pos]);
  }

  // we do not consider overlapping
  char16* utf16_strcpy(char16 *dst, const char16 *src) {
    if (NULL == src || NULL == dst)
      return NULL;

    char16* cp = dst;

    while ((char16)'\0' != *src) {
      *cp = *src;
      cp++;
      src++;
    }

    *cp = *src;

    return dst;
  }

  char16* utf16_strncpy(char16 *dst, const char16 *src, size_t size) {
    if (NULL == src || NULL == dst || 0 == size)
      return NULL;

    if (src == dst)
      return dst;

    char16* cp = dst;

    if (dst < src || (dst > src && dst >= src + size)) {
      while (size-- && (*cp++ = *src++))
        ;
    } else {
      cp += size - 1;
      src += size - 1;
      while (size-- && (*cp-- == *src--))
        ;
    }
    return dst;
  }

  // We do not handle complicated cases like overlapping, because in this
  // codebase, it is not necessary.
  char* utf16_strcpy_tochar(char *dst, const char16 *src) {
    if (NULL == src || NULL == dst)
      return NULL;

    char* cp = dst;

    while ((char16)'\0' != *src) {
      *cp = static_cast<char>(*src);
      cp++;
      src++;
    }
    *cp = *src;

    return dst;
  }

#ifdef __cplusplus
}
#endif
}  // namespace ime_pinyin