~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/extensions/xmlterm/lineterm/unistring.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The contents of this file are subject to the Mozilla Public
 
3
 * License Version 1.1 (the "MPL"); you may not use this file
 
4
 * except in compliance with the MPL. You may obtain a copy of
 
5
 * the MPL at http://www.mozilla.org/MPL/
 
6
 * 
 
7
 * Software distributed under the MPL is distributed on an "AS
 
8
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
9
 * implied. See the MPL for the specific language governing
 
10
 * rights and limitations under the MPL.
 
11
 * 
 
12
 * The Original Code is lineterm.
 
13
 * 
 
14
 * The Initial Developer of the Original Code is Ramalingam Saravanan.
 
15
 * Portions created by Ramalingam Saravanan <svn@xmlterm.org> are
 
16
 * Copyright (C) 1999 Ramalingam Saravanan. All Rights Reserved.
 
17
 * 
 
18
 * Contributor(s):
 
19
 * 
 
20
 * Alternatively, the contents of this file may be used under the
 
21
 * terms of the GNU General Public License (the "GPL"), in which case
 
22
 * the provisions of the GPL are applicable instead of
 
23
 * those above. If you wish to allow use of your version of this
 
24
 * file only under the terms of the GPL and not to allow
 
25
 * others to use your version of this file under the MPL, indicate
 
26
 * your decision by deleting the provisions above and replace them
 
27
 * with the notice and other provisions required by the GPL.
 
28
 * If you do not delete the provisions above, a recipient
 
29
 * may use your version of this file under either the MPL or the
 
30
 * GPL.
 
31
 */
 
32
 
 
33
/* unistring.c: Unicode string operations implementation */
 
34
 
 
35
/* public declarations */
 
36
#include "unistring.h"
 
37
 
 
38
/* private declarations */
 
39
 
 
40
/** Encodes Unicode string US with NUS characters into UTF8 string S with
 
41
 * upto NS characters, returning the number of REMAINING Unicode characters
 
42
 * and the number of ENCODED Utf8 characters
 
43
 */
 
44
void ucstoutf8(const UNICHAR* us, int nus, char* s, int ns, 
 
45
               int* remaining, int* encoded)
 
46
{
 
47
  int j, k;
 
48
 
 
49
  j = 0;
 
50
  k = 0;
 
51
  while ((j < ns) && (k < nus)) {
 
52
    UNICHAR uch = us[k++];
 
53
 
 
54
    if (uch < 0x0080) {
 
55
      s[j++] = uch;
 
56
 
 
57
    } else if (uch < 0x0800) {
 
58
      if (j >= ns-1) break;
 
59
      s[j++] = ((uch & 0x07C0) >>  6) | 0xC0;
 
60
      s[j++] =  (uch & 0x003F)        | 0x80;
 
61
 
 
62
    } else {
 
63
      if (j >= ns-2) break;
 
64
      s[j++] = ((uch & 0xF000) >> 12) | 0xE0;
 
65
      s[j++] = ((uch & 0x0FC0) >>  6) | 0x80;
 
66
      s[j++] =  (uch & 0x003F)        | 0x80;
 
67
    }
 
68
  }
 
69
 
 
70
  if (remaining)
 
71
    *remaining = nus - k;
 
72
 
 
73
  if (encoded)
 
74
    *encoded = j;
 
75
}
 
76
 
 
77
 
 
78
/** Decodes UTF8 string S with NS characters to Unicode string US with
 
79
 * upto NUS characters, returning the number of REMAINING Utf8 characters
 
80
 * and the number of DECODED Unicode characters.
 
81
 * If skipNUL is non-zero, NUL input characters are skipped.
 
82
 * returns 0 if successful,
 
83
 *        -1 if an error occurred during decoding
 
84
 */
 
85
int utf8toucs(const char* s, int ns, UNICHAR* us, int nus,
 
86
              int skipNUL, int* remaining, int* decoded)
 
87
{
 
88
  int j, k;
 
89
  int retcode = 0;
 
90
 
 
91
  j = 0;
 
92
  k = 0;
 
93
  while ((j < ns) && (k < nus)) {
 
94
    char ch = s[j];
 
95
 
 
96
    if (0x80 & ch) {
 
97
      if (0x40 & ch) {
 
98
        if (0x20 & ch) {
 
99
          /* consume 3 */
 
100
          if (j >= ns-2) break;
 
101
 
 
102
          if ( (s[j+1] & 0x40) || !(s[j+1] & 0x80) ||
 
103
               (s[j+2] & 0x40) || !(s[j+2] & 0x80) ) {
 
104
            retcode = -1;
 
105
          }
 
106
 
 
107
          us[k++] =   ((ch     & 0x0F) << 12)
 
108
                    | ((s[j+1] & 0x3F) << 6)
 
109
                    | ( s[j+2] & 0x3F);
 
110
 
 
111
          j += 3;
 
112
 
 
113
        } else {
 
114
          /* consume 2 */
 
115
          if (j >= ns-1) break;
 
116
 
 
117
          if ( (s[j+1] & 0x40) || !(s[j+1] & 0x80) ) {
 
118
            retcode = -1;
 
119
          }
 
120
 
 
121
          us[k++] =   ((ch     & 0x1F) << 6)
 
122
                    | ( s[j+1] & 0x3F);
 
123
          j += 2;
 
124
        }
 
125
 
 
126
      } else {
 
127
        /* consume 1 (error) */
 
128
        retcode = -1;
 
129
        j++;
 
130
      }
 
131
 
 
132
    } else {
 
133
      /* consume 1 */
 
134
      if (ch || !skipNUL) {
 
135
        us[k++] = ch;
 
136
      }
 
137
      j++;
 
138
    }
 
139
  }
 
140
 
 
141
  if (remaining)
 
142
    *remaining = ns - j;
 
143
 
 
144
  if (decoded)
 
145
    *decoded = k;
 
146
 
 
147
  return retcode;
 
148
}
 
149
 
 
150
 
 
151
/** Prints Unicode string US with NUS characters to file stream STREAM,
 
152
 * escaping non-printable ASCII characters and all non-ASCII characters
 
153
 */
 
154
void ucsprint(FILE* stream, const UNICHAR* us, int nus)
 
155
{
 
156
  static const char hexDigits[17] = "0123456789abcdef";
 
157
  UNICHAR uch;
 
158
  int k;
 
159
 
 
160
  for (k=0; k<nus; k++) {
 
161
    uch = us[k];
 
162
 
 
163
    if (uch < (UNICHAR)U_SPACE) {
 
164
      /* ASCII control character */
 
165
      fprintf(stream, "^%c", (char) uch+U_ATSIGN);
 
166
 
 
167
    } else if (uch == (UNICHAR)U_CARET) {
 
168
      /* Caret */
 
169
      fprintf(stream, "^^");
 
170
 
 
171
    } else if (uch < (UNICHAR)U_DEL) {
 
172
      /* Printable ASCII character */
 
173
      fprintf(stream, "%c", (char) uch);
 
174
 
 
175
    } else {
 
176
      /* DEL or non-ASCII character */
 
177
      char esc_str[8]="&#0000;";
 
178
      int j;
 
179
      for (j=5; j>1; j--) {
 
180
        esc_str[j] = hexDigits[uch%16];
 
181
        uch = uch / 16;
 
182
      }
 
183
      fprintf(stream, "%s", esc_str);
 
184
    }
 
185
  }
 
186
}
 
187
 
 
188
 
 
189
/** Copy exactly n characters from plain character source string to UNICHAR
 
190
 * destination string, ignoring source characters past a null character and
 
191
 * padding the destination with null characters if necessary.
 
192
 */
 
193
UNICHAR* ucscopy(register UNICHAR* dest, register const char* srcplain,
 
194
                 size_t n)
 
195
{
 
196
  register UNICHAR ch;
 
197
  register const UNICHAR* destmx = dest + n;
 
198
 
 
199
  /* Copy characters from source to destination, stopping at NUL */
 
200
  while (dest < destmx) {
 
201
    *dest++ = (ch = *srcplain++);
 
202
    if (ch == U_NUL)
 
203
      break;
 
204
  }
 
205
 
 
206
  /* Pad with NULs, if necessary */
 
207
  while (dest < destmx)
 
208
    *dest++ = U_NUL;
 
209
 
 
210
  return dest;
 
211
}
 
212
    
 
213
 
 
214
#ifndef USE_WCHAR
 
215
/** Locates first occurrence of character within string and returns pointer
 
216
 * to it if found, else returning null pointer. (character may be NUL)
 
217
 */
 
218
UNICHAR* ucschr(register const UNICHAR* str, register const UNICHAR chr)
 
219
{
 
220
  do {
 
221
    if (*str == chr)
 
222
      return (UNICHAR*) str;
 
223
  } while (*str++ != U_NUL);
 
224
 
 
225
  return NULL;
 
226
}
 
227
 
 
228
 
 
229
/** Locates last occurrence of character within string and returns pointer
 
230
 * to it if found, else returning null pointer. (character may be NUL)
 
231
 */
 
232
UNICHAR* ucsrchr(register const UNICHAR* str, register const UNICHAR chr)
 
233
{
 
234
  const UNICHAR* retstr = NULL;
 
235
  do {
 
236
    if (*str == chr)
 
237
      retstr = str;
 
238
  } while (*str++ != U_NUL);
 
239
 
 
240
  return (UNICHAR*) retstr;
 
241
}
 
242
 
 
243
 
 
244
/** Compare all characters between string1 and string2, returning
 
245
 * a zero value if all characters are equal, or returning
 
246
 * character1 - character2 for the first character that is different
 
247
 * between the two strings.
 
248
 * (Characters following a null character are not compared.)
 
249
 */
 
250
int ucscmp(register const UNICHAR* str1, register const UNICHAR* str2)
 
251
{
 
252
  register UNICHAR ch1, ch2;
 
253
 
 
254
  do {
 
255
    if ((ch1 = *str1++) != (ch2 = *str2++))
 
256
      return ch1 - ch2;
 
257
 
 
258
  } while (ch1 != U_NUL);
 
259
 
 
260
  return 0;
 
261
}
 
262
 
 
263
    
 
264
/** Compare upto n characters between string1 and string2, returning
 
265
 * a zero value if all compared characters are equal, or returning
 
266
 * character1 - character2 for the first character that is different
 
267
 * between the two strings.
 
268
 * (Characters following a null character are not compared.)
 
269
 */
 
270
int ucsncmp(register const UNICHAR* str1, register const UNICHAR* str2,
 
271
            size_t n)
 
272
{
 
273
  register UNICHAR ch1, ch2;
 
274
  register const UNICHAR* str1mx = str1 + n;
 
275
 
 
276
  while (str1 < str1mx) {
 
277
    if ((ch1 = *str1++) != (ch2 = *str2++))
 
278
      return ch1 - ch2;
 
279
 
 
280
    if (ch1 == U_NUL)
 
281
      break;
 
282
  }
 
283
 
 
284
  return 0;
 
285
}
 
286
 
 
287
    
 
288
/** Copy exactly n characters from source to destination, ignoring source
 
289
 * characters past a null character and padding the destination with null
 
290
 * characters if necessary.
 
291
 */
 
292
UNICHAR* ucsncpy(register UNICHAR* dest, register const UNICHAR* src,
 
293
                 size_t n)
 
294
{
 
295
  register UNICHAR ch;
 
296
  register const UNICHAR* destmx = dest + n;
 
297
 
 
298
  /* Copy characters from source to destination, stopping at NUL */
 
299
  while (dest < destmx) {
 
300
    *dest++ = (ch = *src++);
 
301
    if (ch == U_NUL)
 
302
      break;
 
303
  }
 
304
 
 
305
  /* Pad with NULs, if necessary */
 
306
  while (dest < destmx)
 
307
    *dest++ = U_NUL;
 
308
 
 
309
  return dest;
 
310
}
 
311
    
 
312
 
 
313
/** Returns string length
 
314
 */
 
315
size_t ucslen(const UNICHAR* str)
 
316
{
 
317
  register const UNICHAR* strcp = str;
 
318
 
 
319
  while (*strcp++ != U_NUL);
 
320
 
 
321
  return strcp - str - 1;
 
322
}
 
323
 
 
324
    
 
325
/** Locates substring within string and returns pointer to it if found,
 
326
 * else returning null pointer. If substring has zero length, then full
 
327
 * string is returned.
 
328
 */
 
329
UNICHAR* ucsstr(register const UNICHAR* str, const UNICHAR* substr)
 
330
{
 
331
  register UNICHAR subch1, ch;
 
332
 
 
333
  /* If null substring, return string */
 
334
  if (*substr == U_NUL)
 
335
    return (UNICHAR*) str;
 
336
 
 
337
  /* First character of non-null substring */
 
338
  subch1 = *substr;
 
339
 
 
340
  if ((ch = *str) == U_NUL)
 
341
    return NULL;
 
342
 
 
343
  do {
 
344
 
 
345
    if (ch == subch1) {
 
346
      /* First character matches; check if rest of substring matches */
 
347
      register const UNICHAR* strcp = str;
 
348
      register const UNICHAR* substrcp = substr;
 
349
      do {
 
350
        substrcp++;
 
351
        strcp++;
 
352
        if (*substrcp == U_NUL)
 
353
          return (UNICHAR*) str;
 
354
      } while (*substrcp == *strcp);
 
355
    }
 
356
 
 
357
  } while ((ch = *(++str)) != U_NUL);
 
358
 
 
359
  return NULL;
 
360
}
 
361
    
 
362
 
 
363
/** Returns length of longest initial segment of string that contains
 
364
 * only the specified characters.
 
365
 */
 
366
size_t ucsspn(const UNICHAR* str, const UNICHAR* chars)
 
367
{
 
368
  register UNICHAR strch, ch;
 
369
  register const UNICHAR* charscp;
 
370
  register const UNICHAR* strcp = str;
 
371
 
 
372
  while ((strch = *strcp++) != U_NUL) {
 
373
    charscp = chars;
 
374
 
 
375
    /* Check that it is one of the specified characters */
 
376
    while ((ch = *charscp++) != U_NUL) {
 
377
      if (strch == ch)
 
378
        break;
 
379
    }
 
380
    if (ch == U_NUL)
 
381
      return (size_t) (strcp - str - 1);
 
382
  }
 
383
 
 
384
  return (size_t) (strcp - str - 1);
 
385
}
 
386
    
 
387
 
 
388
/** Returns length of longest initial segment of string that does not
 
389
 * contain any of the specified characters.
 
390
 */
 
391
size_t ucscspn(const UNICHAR* str, const UNICHAR* chars)
 
392
{
 
393
  register UNICHAR strch, ch;
 
394
  register const UNICHAR* charscp;
 
395
  register const UNICHAR* strcp = str;
 
396
 
 
397
  while ((strch = *strcp++) != U_NUL) {
 
398
    charscp = chars;
 
399
 
 
400
    /* Check that it is not one of the specified characters */
 
401
    while ((ch = *charscp++) != U_NUL) {
 
402
      if (strch == ch)
 
403
        return (size_t) (strcp - str - 1);
 
404
    }
 
405
  }
 
406
 
 
407
  return (size_t) (strcp - str - 1);
 
408
}
 
409
#endif  /* !USE_WCHAR */