OLD | NEW |
1 /* | 1 /* |
2 ******************************************************************************* | 2 ******************************************************************************* |
3 * | 3 * |
4 * Copyright (C) 2000-2002, International Business Machines | 4 * Copyright (C) 2000-2014, International Business Machines |
5 * Corporation and others. All Rights Reserved. | 5 * Corporation and others. All Rights Reserved. |
6 * | 6 * |
7 ******************************************************************************* | 7 ******************************************************************************* |
8 * file name: ustring.c | 8 * file name: ustring.c |
9 * encoding: US-ASCII | 9 * encoding: US-ASCII |
10 * tab size: 8 (not used) | 10 * tab size: 8 (not used) |
11 * indentation:4 | 11 * indentation:4 |
12 * | 12 * |
13 * created on: 2000aug15 | 13 * created on: 2000aug15 |
14 * created by: Markus W. Scherer | 14 * created by: Markus W. Scherer |
15 * | 15 * |
16 * This file contains sample code that illustrates the use of Unicode strings | 16 * This file contains sample code that illustrates the use of Unicode strings |
17 * with ICU. | 17 * with ICU. |
18 */ | 18 */ |
19 | 19 |
20 #include <stdio.h> | 20 #include <stdio.h> |
21 #include "unicode/utypes.h" | 21 #include "unicode/utypes.h" |
22 #include "unicode/uchar.h" | 22 #include "unicode/uchar.h" |
23 #include "unicode/locid.h" | 23 #include "unicode/locid.h" |
24 #include "unicode/ustring.h" | 24 #include "unicode/ustring.h" |
25 #include "unicode/ucnv.h" | 25 #include "unicode/ucnv.h" |
26 #include "unicode/unistr.h" | 26 #include "unicode/unistr.h" |
27 | 27 |
28 #define LENGTHOF(array) (sizeof(array)/sizeof((array)[0])) | |
29 | |
30 // helper functions -------------------------------------------------------- *** | 28 // helper functions -------------------------------------------------------- *** |
31 | 29 |
32 // default converter for the platform encoding | 30 // default converter for the platform encoding |
33 static UConverter *cnv=NULL; | 31 static UConverter *cnv=NULL; |
34 | 32 |
35 static void | 33 static void |
36 printUString(const char *announce, const UChar *s, int32_t length) { | 34 printUString(const char *announce, const UChar *s, int32_t length) { |
37 static char out[200]; | 35 static char out[200]; |
38 UChar32 c; | 36 UChar32 c; |
39 int32_t i; | 37 int32_t i; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 | 98 |
101 static void | 99 static void |
102 demo_utf_h_macros() { | 100 demo_utf_h_macros() { |
103 static UChar input[]={ 0x0061, 0xd800, 0xdc00, 0xdbff, 0xdfff, 0x0062 }; | 101 static UChar input[]={ 0x0061, 0xd800, 0xdc00, 0xdbff, 0xdfff, 0x0062 }; |
104 UChar32 c; | 102 UChar32 c; |
105 int32_t i; | 103 int32_t i; |
106 UBool isError; | 104 UBool isError; |
107 | 105 |
108 printf("\n* demo_utf_h_macros() -------------- ***\n\n"); | 106 printf("\n* demo_utf_h_macros() -------------- ***\n\n"); |
109 | 107 |
110 printUString("iterate forward through: ", input, LENGTHOF(input)); | 108 printUString("iterate forward through: ", input, UPRV_LENGTHOF(input)); |
111 for(i=0; i<LENGTHOF(input); /* U16_NEXT post-increments */) { | 109 for(i=0; i<UPRV_LENGTHOF(input); /* U16_NEXT post-increments */) { |
112 /* Iterating forwards | 110 /* Iterating forwards |
113 Codepoint at offset 0: U+0061 | 111 Codepoint at offset 0: U+0061 |
114 Codepoint at offset 1: U+10000 | 112 Codepoint at offset 1: U+10000 |
115 Codepoint at offset 3: U+10ffff | 113 Codepoint at offset 3: U+10ffff |
116 Codepoint at offset 5: U+0062 | 114 Codepoint at offset 5: U+0062 |
117 */ | 115 */ |
118 printf("Codepoint at offset %d: U+", i); | 116 printf("Codepoint at offset %d: U+", i); |
119 U16_NEXT(input, i, LENGTHOF(input), c); | 117 U16_NEXT(input, i, UPRV_LENGTHOF(input), c); |
120 printf("%04x\n", c); | 118 printf("%04x\n", c); |
121 } | 119 } |
122 | 120 |
123 puts(""); | 121 puts(""); |
124 | 122 |
125 isError=FALSE; | 123 isError=FALSE; |
126 i=1; /* write position, gets post-incremented so needs to be in an l-value *
/ | 124 i=1; /* write position, gets post-incremented so needs to be in an l-value *
/ |
127 U16_APPEND(input, i, LENGTHOF(input), 0x0062, isError); | 125 U16_APPEND(input, i, UPRV_LENGTHOF(input), 0x0062, isError); |
128 | 126 |
129 printUString("iterate backward through: ", input, LENGTHOF(input)); | 127 printUString("iterate backward through: ", input, UPRV_LENGTHOF(input)); |
130 for(i=LENGTHOF(input); i>0; /* U16_PREV pre-decrements */) { | 128 for(i=UPRV_LENGTHOF(input); i>0; /* U16_PREV pre-decrements */) { |
131 U16_PREV(input, 0, i, c); | 129 U16_PREV(input, 0, i, c); |
132 /* Iterating backwards | 130 /* Iterating backwards |
133 Codepoint at offset 5: U+0062 | 131 Codepoint at offset 5: U+0062 |
134 Codepoint at offset 3: U+10ffff | 132 Codepoint at offset 3: U+10ffff |
135 Codepoint at offset 2: U+dc00 -- unpaired surrogate because lead surr
. overwritten | 133 Codepoint at offset 2: U+dc00 -- unpaired surrogate because lead surr
. overwritten |
136 Codepoint at offset 1: U+0062 -- by this BMP code point | 134 Codepoint at offset 1: U+0062 -- by this BMP code point |
137 Codepoint at offset 0: U+0061 | 135 Codepoint at offset 0: U+0061 |
138 */ | 136 */ |
139 printf("Codepoint at offset %d: U+%04x\n", i, c); | 137 printf("Codepoint at offset %d: U+%04x\n", i, c); |
140 } | 138 } |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 * 1:1 code point mappings without context/locale ID. | 205 * 1:1 code point mappings without context/locale ID. |
208 * | 206 * |
209 * Note that some mappings will not be "right" because some "real" | 207 * Note that some mappings will not be "right" because some "real" |
210 * case mappings require context, depend on the locale ID, | 208 * case mappings require context, depend on the locale ID, |
211 * and/or result in a change in the number of code points. | 209 * and/or result in a change in the number of code points. |
212 */ | 210 */ |
213 printUString("input string: ", input, -1); | 211 printUString("input string: ", input, -1); |
214 | 212 |
215 /* uppercase */ | 213 /* uppercase */ |
216 isError=FALSE; | 214 isError=FALSE; |
217 for(i=j=0; j<LENGTHOF(buffer) && !isError; /* U16_NEXT post-increments */) { | 215 for(i=j=0; j<UPRV_LENGTHOF(buffer) && !isError; /* U16_NEXT post-increments
*/) { |
218 U16_NEXT(input, i, INT32_MAX, c); /* without length because NUL-terminat
ed */ | 216 U16_NEXT(input, i, INT32_MAX, c); /* without length because NUL-terminat
ed */ |
219 if(c==0) { | 217 if(c==0) { |
220 break; /* stop at terminating NUL, no need to terminate buffer */ | 218 break; /* stop at terminating NUL, no need to terminate buffer */ |
221 } | 219 } |
222 c=u_toupper(c); | 220 c=u_toupper(c); |
223 U16_APPEND(buffer, j, LENGTHOF(buffer), c, isError); | 221 U16_APPEND(buffer, j, UPRV_LENGTHOF(buffer), c, isError); |
224 } | 222 } |
225 printUString("simple-uppercased: ", buffer, j); | 223 printUString("simple-uppercased: ", buffer, j); |
226 /* lowercase */ | 224 /* lowercase */ |
227 isError=FALSE; | 225 isError=FALSE; |
228 for(i=j=0; j<LENGTHOF(buffer) && !isError; /* U16_NEXT post-increments */) { | 226 for(i=j=0; j<UPRV_LENGTHOF(buffer) && !isError; /* U16_NEXT post-increments
*/) { |
229 U16_NEXT(input, i, INT32_MAX, c); /* without length because NUL-terminat
ed */ | 227 U16_NEXT(input, i, INT32_MAX, c); /* without length because NUL-terminat
ed */ |
230 if(c==0) { | 228 if(c==0) { |
231 break; /* stop at terminating NUL, no need to terminate buffer */ | 229 break; /* stop at terminating NUL, no need to terminate buffer */ |
232 } | 230 } |
233 c=u_tolower(c); | 231 c=u_tolower(c); |
234 U16_APPEND(buffer, j, LENGTHOF(buffer), c, isError); | 232 U16_APPEND(buffer, j, UPRV_LENGTHOF(buffer), c, isError); |
235 } | 233 } |
236 printUString("simple-lowercased: ", buffer, j); | 234 printUString("simple-lowercased: ", buffer, j); |
237 /* titlecase */ | 235 /* titlecase */ |
238 isError=FALSE; | 236 isError=FALSE; |
239 for(i=j=0; j<LENGTHOF(buffer) && !isError; /* U16_NEXT post-increments */) { | 237 for(i=j=0; j<UPRV_LENGTHOF(buffer) && !isError; /* U16_NEXT post-increments
*/) { |
240 U16_NEXT(input, i, INT32_MAX, c); /* without length because NUL-terminat
ed */ | 238 U16_NEXT(input, i, INT32_MAX, c); /* without length because NUL-terminat
ed */ |
241 if(c==0) { | 239 if(c==0) { |
242 break; /* stop at terminating NUL, no need to terminate buffer */ | 240 break; /* stop at terminating NUL, no need to terminate buffer */ |
243 } | 241 } |
244 c=u_totitle(c); | 242 c=u_totitle(c); |
245 U16_APPEND(buffer, j, LENGTHOF(buffer), c, isError); | 243 U16_APPEND(buffer, j, UPRV_LENGTHOF(buffer), c, isError); |
246 } | 244 } |
247 printUString("simple-titlecased: ", buffer, j); | 245 printUString("simple-titlecased: ", buffer, j); |
248 /* case-fold/default */ | 246 /* case-fold/default */ |
249 isError=FALSE; | 247 isError=FALSE; |
250 for(i=j=0; j<LENGTHOF(buffer) && !isError; /* U16_NEXT post-increments */) { | 248 for(i=j=0; j<UPRV_LENGTHOF(buffer) && !isError; /* U16_NEXT post-increments
*/) { |
251 U16_NEXT(input, i, INT32_MAX, c); /* without length because NUL-terminat
ed */ | 249 U16_NEXT(input, i, INT32_MAX, c); /* without length because NUL-terminat
ed */ |
252 if(c==0) { | 250 if(c==0) { |
253 break; /* stop at terminating NUL, no need to terminate buffer */ | 251 break; /* stop at terminating NUL, no need to terminate buffer */ |
254 } | 252 } |
255 c=u_foldCase(c, U_FOLD_CASE_DEFAULT); | 253 c=u_foldCase(c, U_FOLD_CASE_DEFAULT); |
256 U16_APPEND(buffer, j, LENGTHOF(buffer), c, isError); | 254 U16_APPEND(buffer, j, UPRV_LENGTHOF(buffer), c, isError); |
257 } | 255 } |
258 printUString("simple-case-folded/default: ", buffer, j); | 256 printUString("simple-case-folded/default: ", buffer, j); |
259 /* case-fold/Turkic */ | 257 /* case-fold/Turkic */ |
260 isError=FALSE; | 258 isError=FALSE; |
261 for(i=j=0; j<LENGTHOF(buffer) && !isError; /* U16_NEXT post-increments */) { | 259 for(i=j=0; j<UPRV_LENGTHOF(buffer) && !isError; /* U16_NEXT post-increments
*/) { |
262 U16_NEXT(input, i, INT32_MAX, c); /* without length because NUL-terminat
ed */ | 260 U16_NEXT(input, i, INT32_MAX, c); /* without length because NUL-terminat
ed */ |
263 if(c==0) { | 261 if(c==0) { |
264 break; /* stop at terminating NUL, no need to terminate buffer */ | 262 break; /* stop at terminating NUL, no need to terminate buffer */ |
265 } | 263 } |
266 c=u_foldCase(c, U_FOLD_CASE_EXCLUDE_SPECIAL_I); | 264 c=u_foldCase(c, U_FOLD_CASE_EXCLUDE_SPECIAL_I); |
267 U16_APPEND(buffer, j, LENGTHOF(buffer), c, isError); | 265 U16_APPEND(buffer, j, UPRV_LENGTHOF(buffer), c, isError); |
268 } | 266 } |
269 printUString("simple-case-folded/Turkic: ", buffer, j); | 267 printUString("simple-case-folded/Turkic: ", buffer, j); |
270 | 268 |
271 /* | 269 /* |
272 * Second, use full case mapping functions which provide | 270 * Second, use full case mapping functions which provide |
273 * 1:n code point mappings (n can be 0!) and are sensitive to context and lo
cale ID. | 271 * 1:n code point mappings (n can be 0!) and are sensitive to context and lo
cale ID. |
274 * | 272 * |
275 * Note that lower/upper/titlecasing take a locale ID while case-folding | 273 * Note that lower/upper/titlecasing take a locale ID while case-folding |
276 * has bit flag options instead, by design of the Unicode SpecialCasing.txt
UCD file. | 274 * has bit flag options instead, by design of the Unicode SpecialCasing.txt
UCD file. |
277 * | 275 * |
278 * Also, string titlecasing requires a BreakIterator to find starts of words
. | 276 * Also, string titlecasing requires a BreakIterator to find starts of words
. |
279 * The sample code here passes in a NULL pointer; u_strToTitle() will open a
nd close a default | 277 * The sample code here passes in a NULL pointer; u_strToTitle() will open a
nd close a default |
280 * titlecasing BreakIterator automatically. | 278 * titlecasing BreakIterator automatically. |
281 * For production code where many strings are titlecased it would be more ef
ficient | 279 * For production code where many strings are titlecased it would be more ef
ficient |
282 * to open a BreakIterator externally and pass it in. | 280 * to open a BreakIterator externally and pass it in. |
283 */ | 281 */ |
284 printUString("\ninput string: ", input, -1); | 282 printUString("\ninput string: ", input, -1); |
285 | 283 |
286 /* lowercase/English */ | 284 /* lowercase/English */ |
287 errorCode=U_ZERO_ERROR; | 285 errorCode=U_ZERO_ERROR; |
288 length=u_strToLower(buffer, LENGTHOF(buffer), input, -1, "en", &errorCode); | 286 length=u_strToLower(buffer, UPRV_LENGTHOF(buffer), input, -1, "en", &errorCo
de); |
289 if(U_SUCCESS(errorCode)) { | 287 if(U_SUCCESS(errorCode)) { |
290 printUString("full-lowercased/en: ", buffer, length); | 288 printUString("full-lowercased/en: ", buffer, length); |
291 } else { | 289 } else { |
292 printf("error in u_strToLower(en)=%ld error=%s\n", length, u_errorName(e
rrorCode)); | 290 printf("error in u_strToLower(en)=%ld error=%s\n", length, u_errorName(e
rrorCode)); |
293 } | 291 } |
294 /* lowercase/Turkish */ | 292 /* lowercase/Turkish */ |
295 errorCode=U_ZERO_ERROR; | 293 errorCode=U_ZERO_ERROR; |
296 length=u_strToLower(buffer, LENGTHOF(buffer), input, -1, "tr", &errorCode); | 294 length=u_strToLower(buffer, UPRV_LENGTHOF(buffer), input, -1, "tr", &errorCo
de); |
297 if(U_SUCCESS(errorCode)) { | 295 if(U_SUCCESS(errorCode)) { |
298 printUString("full-lowercased/tr: ", buffer, length); | 296 printUString("full-lowercased/tr: ", buffer, length); |
299 } else { | 297 } else { |
300 printf("error in u_strToLower(tr)=%ld error=%s\n", length, u_errorName(e
rrorCode)); | 298 printf("error in u_strToLower(tr)=%ld error=%s\n", length, u_errorName(e
rrorCode)); |
301 } | 299 } |
302 /* uppercase/English */ | 300 /* uppercase/English */ |
303 errorCode=U_ZERO_ERROR; | 301 errorCode=U_ZERO_ERROR; |
304 length=u_strToUpper(buffer, LENGTHOF(buffer), input, -1, "en", &errorCode); | 302 length=u_strToUpper(buffer, UPRV_LENGTHOF(buffer), input, -1, "en", &errorCo
de); |
305 if(U_SUCCESS(errorCode)) { | 303 if(U_SUCCESS(errorCode)) { |
306 printUString("full-uppercased/en: ", buffer, length); | 304 printUString("full-uppercased/en: ", buffer, length); |
307 } else { | 305 } else { |
308 printf("error in u_strToUpper(en)=%ld error=%s\n", length, u_errorName(e
rrorCode)); | 306 printf("error in u_strToUpper(en)=%ld error=%s\n", length, u_errorName(e
rrorCode)); |
309 } | 307 } |
310 /* uppercase/Turkish */ | 308 /* uppercase/Turkish */ |
311 errorCode=U_ZERO_ERROR; | 309 errorCode=U_ZERO_ERROR; |
312 length=u_strToUpper(buffer, LENGTHOF(buffer), input, -1, "tr", &errorCode); | 310 length=u_strToUpper(buffer, UPRV_LENGTHOF(buffer), input, -1, "tr", &errorCo
de); |
313 if(U_SUCCESS(errorCode)) { | 311 if(U_SUCCESS(errorCode)) { |
314 printUString("full-uppercased/tr: ", buffer, length); | 312 printUString("full-uppercased/tr: ", buffer, length); |
315 } else { | 313 } else { |
316 printf("error in u_strToUpper(tr)=%ld error=%s\n", length, u_errorName(e
rrorCode)); | 314 printf("error in u_strToUpper(tr)=%ld error=%s\n", length, u_errorName(e
rrorCode)); |
317 } | 315 } |
318 /* titlecase/English */ | 316 /* titlecase/English */ |
319 errorCode=U_ZERO_ERROR; | 317 errorCode=U_ZERO_ERROR; |
320 length=u_strToTitle(buffer, LENGTHOF(buffer), input, -1, NULL, "en", &errorC
ode); | 318 length=u_strToTitle(buffer, UPRV_LENGTHOF(buffer), input, -1, NULL, "en", &e
rrorCode); |
321 if(U_SUCCESS(errorCode)) { | 319 if(U_SUCCESS(errorCode)) { |
322 printUString("full-titlecased/en: ", buffer, length); | 320 printUString("full-titlecased/en: ", buffer, length); |
323 } else { | 321 } else { |
324 printf("error in u_strToTitle(en)=%ld error=%s\n", length, u_errorName(e
rrorCode)); | 322 printf("error in u_strToTitle(en)=%ld error=%s\n", length, u_errorName(e
rrorCode)); |
325 } | 323 } |
326 /* titlecase/Turkish */ | 324 /* titlecase/Turkish */ |
327 errorCode=U_ZERO_ERROR; | 325 errorCode=U_ZERO_ERROR; |
328 length=u_strToTitle(buffer, LENGTHOF(buffer), input, -1, NULL, "tr", &errorC
ode); | 326 length=u_strToTitle(buffer, UPRV_LENGTHOF(buffer), input, -1, NULL, "tr", &e
rrorCode); |
329 if(U_SUCCESS(errorCode)) { | 327 if(U_SUCCESS(errorCode)) { |
330 printUString("full-titlecased/tr: ", buffer, length); | 328 printUString("full-titlecased/tr: ", buffer, length); |
331 } else { | 329 } else { |
332 printf("error in u_strToTitle(tr)=%ld error=%s\n", length, u_errorName(e
rrorCode)); | 330 printf("error in u_strToTitle(tr)=%ld error=%s\n", length, u_errorName(e
rrorCode)); |
333 } | 331 } |
334 /* case-fold/default */ | 332 /* case-fold/default */ |
335 errorCode=U_ZERO_ERROR; | 333 errorCode=U_ZERO_ERROR; |
336 length=u_strFoldCase(buffer, LENGTHOF(buffer), input, -1, U_FOLD_CASE_DEFAUL
T, &errorCode); | 334 length=u_strFoldCase(buffer, UPRV_LENGTHOF(buffer), input, -1, U_FOLD_CASE_D
EFAULT, &errorCode); |
337 if(U_SUCCESS(errorCode)) { | 335 if(U_SUCCESS(errorCode)) { |
338 printUString("full-case-folded/default: ", buffer, length); | 336 printUString("full-case-folded/default: ", buffer, length); |
339 } else { | 337 } else { |
340 printf("error in u_strFoldCase(default)=%ld error=%s\n", length, u_error
Name(errorCode)); | 338 printf("error in u_strFoldCase(default)=%ld error=%s\n", length, u_error
Name(errorCode)); |
341 } | 339 } |
342 /* case-fold/Turkic */ | 340 /* case-fold/Turkic */ |
343 errorCode=U_ZERO_ERROR; | 341 errorCode=U_ZERO_ERROR; |
344 length=u_strFoldCase(buffer, LENGTHOF(buffer), input, -1, U_FOLD_CASE_EXCLUD
E_SPECIAL_I, &errorCode); | 342 length=u_strFoldCase(buffer, UPRV_LENGTHOF(buffer), input, -1, U_FOLD_CASE_E
XCLUDE_SPECIAL_I, &errorCode); |
345 if(U_SUCCESS(errorCode)) { | 343 if(U_SUCCESS(errorCode)) { |
346 printUString("full-case-folded/Turkic: ", buffer, length); | 344 printUString("full-case-folded/Turkic: ", buffer, length); |
347 } else { | 345 } else { |
348 printf("error in u_strFoldCase(Turkic)=%ld error=%s\n", length, u_errorN
ame(errorCode)); | 346 printf("error in u_strFoldCase(Turkic)=%ld error=%s\n", length, u_errorN
ame(errorCode)); |
349 } | 347 } |
350 } | 348 } |
351 | 349 |
352 // sample code for case mappings with C++ APIs ------------------------------ **
* | 350 // sample code for case mappings with C++ APIs ------------------------------ **
* |
353 | 351 |
354 static void demoCaseMapInCPlusPlus() { | 352 static void demoCaseMapInCPlusPlus() { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 | 419 |
422 // * UnicodeString with internally stored contents | 420 // * UnicodeString with internally stored contents |
423 // instantiate a UnicodeString from a single code point | 421 // instantiate a UnicodeString from a single code point |
424 // the few (2) UChars will be stored in the object itself | 422 // the few (2) UChars will be stored in the object itself |
425 UnicodeString one((UChar32)0x24001); | 423 UnicodeString one((UChar32)0x24001); |
426 // this copies the few UChars into the "two" object | 424 // this copies the few UChars into the "two" object |
427 UnicodeString two=one; | 425 UnicodeString two=one; |
428 printf("length of short string copy: %d\n", two.length()); | 426 printf("length of short string copy: %d\n", two.length()); |
429 // set "one" to contain the 3 UChars from readonly | 427 // set "one" to contain the 3 UChars from readonly |
430 // this setTo() variant copies the characters | 428 // this setTo() variant copies the characters |
431 one.setTo(readonly, LENGTHOF(readonly)); | 429 one.setTo(readonly, UPRV_LENGTHOF(readonly)); |
432 | 430 |
433 // * UnicodeString with allocated contents | 431 // * UnicodeString with allocated contents |
434 // build a longer string that will not fit into the object's buffer | 432 // build a longer string that will not fit into the object's buffer |
435 one+=UnicodeString(writeable, LENGTHOF(writeable)); | 433 one+=UnicodeString(writeable, UPRV_LENGTHOF(writeable)); |
436 one+=one; | 434 one+=one; |
437 one+=one; | 435 one+=one; |
438 printf("length of longer string: %d\n", one.length()); | 436 printf("length of longer string: %d\n", one.length()); |
439 // copying will use the same allocated buffer and increment the reference | 437 // copying will use the same allocated buffer and increment the reference |
440 // counter | 438 // counter |
441 two=one; | 439 two=one; |
442 printf("length of longer string copy: %d\n", two.length()); | 440 printf("length of longer string copy: %d\n", two.length()); |
443 | 441 |
444 // * UnicodeString using readonly-alias to a const UChar array | 442 // * UnicodeString using readonly-alias to a const UChar array |
445 // construct a string that aliases a readonly buffer | 443 // construct a string that aliases a readonly buffer |
446 UnicodeString three(FALSE, readonly, LENGTHOF(readonly)); | 444 UnicodeString three(FALSE, readonly, UPRV_LENGTHOF(readonly)); |
447 printUnicodeString("readonly-alias string: ", three); | 445 printUnicodeString("readonly-alias string: ", three); |
448 // copy-on-write: any modification to the string results in | 446 // copy-on-write: any modification to the string results in |
449 // a copy to either the internal buffer or to a newly allocated one | 447 // a copy to either the internal buffer or to a newly allocated one |
450 three.setCharAt(1, 0x39); | 448 three.setCharAt(1, 0x39); |
451 printUnicodeString("readonly-aliasing string after modification: ", three); | 449 printUnicodeString("readonly-aliasing string after modification: ", three); |
452 // the aliased array is not modified | 450 // the aliased array is not modified |
453 for(i=0; i<three.length(); ++i) { | 451 for(i=0; i<three.length(); ++i) { |
454 printf("readonly buffer[%d] after modifying its string: 0x%lx\n", | 452 printf("readonly buffer[%d] after modifying its string: 0x%lx\n", |
455 i, readonly[i]); | 453 i, readonly[i]); |
456 } | 454 } |
457 // setTo() readonly alias | 455 // setTo() readonly alias |
458 one.setTo(FALSE, writeable, LENGTHOF(writeable)); | 456 one.setTo(FALSE, writeable, UPRV_LENGTHOF(writeable)); |
459 // copying the readonly-alias object with fastCopyFrom() (new in ICU 2.4) | 457 // copying the readonly-alias object with fastCopyFrom() (new in ICU 2.4) |
460 // will readonly-alias the same buffer | 458 // will readonly-alias the same buffer |
461 two.fastCopyFrom(one); | 459 two.fastCopyFrom(one); |
462 printUnicodeString("fastCopyFrom(readonly alias of \"writeable\" array): ",
two); | 460 printUnicodeString("fastCopyFrom(readonly alias of \"writeable\" array): ",
two); |
463 printf("verify that a fastCopyFrom(readonly alias) uses the same buffer poin
ter: %d (should be 1)\n", | 461 printf("verify that a fastCopyFrom(readonly alias) uses the same buffer poin
ter: %d (should be 1)\n", |
464 one.getBuffer()==two.getBuffer()); | 462 one.getBuffer()==two.getBuffer()); |
465 // a normal assignment will clone the contents (new in ICU 2.4) | 463 // a normal assignment will clone the contents (new in ICU 2.4) |
466 two=one; | 464 two=one; |
467 printf("verify that a regular copy of a readonly alias uses a different buff
er pointer: %d (should be 0)\n", | 465 printf("verify that a regular copy of a readonly alias uses a different buff
er pointer: %d (should be 0)\n", |
468 one.getBuffer()==two.getBuffer()); | 466 one.getBuffer()==two.getBuffer()); |
469 | 467 |
470 // * UnicodeString using writeable-alias to a non-const UChar array | 468 // * UnicodeString using writeable-alias to a non-const UChar array |
471 UnicodeString four(writeable, LENGTHOF(writeable), LENGTHOF(writeable)); | 469 UnicodeString four(writeable, UPRV_LENGTHOF(writeable), UPRV_LENGTHOF(writea
ble)); |
472 printUnicodeString("writeable-alias string: ", four); | 470 printUnicodeString("writeable-alias string: ", four); |
473 // a modification writes through to the buffer | 471 // a modification writes through to the buffer |
474 four.setCharAt(1, 0x39); | 472 four.setCharAt(1, 0x39); |
475 for(i=0; i<four.length(); ++i) { | 473 for(i=0; i<four.length(); ++i) { |
476 printf("writeable-alias backing buffer[%d]=0x%lx " | 474 printf("writeable-alias backing buffer[%d]=0x%lx " |
477 "after modification\n", i, writeable[i]); | 475 "after modification\n", i, writeable[i]); |
478 } | 476 } |
479 // a copy will not alias any more; | 477 // a copy will not alias any more; |
480 // instead, it will get a copy of the contents into allocated memory | 478 // instead, it will get a copy of the contents into allocated memory |
481 two=four; | 479 two=four; |
482 two.setCharAt(1, 0x21); | 480 two.setCharAt(1, 0x21); |
483 for(i=0; i<two.length(); ++i) { | 481 for(i=0; i<two.length(); ++i) { |
484 printf("writeable-alias backing buffer[%d]=0x%lx after " | 482 printf("writeable-alias backing buffer[%d]=0x%lx after " |
485 "modification of string copy\n", i, writeable[i]); | 483 "modification of string copy\n", i, writeable[i]); |
486 } | 484 } |
487 // setTo() writeable alias, capacity==length | 485 // setTo() writeable alias, capacity==length |
488 one.setTo(writeable, LENGTHOF(writeable), LENGTHOF(writeable)); | 486 one.setTo(writeable, UPRV_LENGTHOF(writeable), UPRV_LENGTHOF(writeable)); |
489 // grow the string - it will not fit into the backing buffer any more | 487 // grow the string - it will not fit into the backing buffer any more |
490 // and will get copied before modification | 488 // and will get copied before modification |
491 one.append((UChar)0x40); | 489 one.append((UChar)0x40); |
492 // shrink it back so it would fit | 490 // shrink it back so it would fit |
493 one.truncate(one.length()-1); | 491 one.truncate(one.length()-1); |
494 // we still operate on the copy | 492 // we still operate on the copy |
495 one.setCharAt(1, 0x25); | 493 one.setCharAt(1, 0x25); |
496 printf("string after growing too much and then shrinking[1]=0x%lx\n" | 494 printf("string after growing too much and then shrinking[1]=0x%lx\n" |
497 " backing store for this[1]=0x%lx\n", | 495 " backing store for this[1]=0x%lx\n", |
498 one.charAt(1), writeable[1]); | 496 one.charAt(1), writeable[1]); |
499 // if we need it in the original buffer, then extract() to it | 497 // if we need it in the original buffer, then extract() to it |
500 // extract() does not do anything if the string aliases that same buffer | 498 // extract() does not do anything if the string aliases that same buffer |
501 // i=min(one.length(), length of array) | 499 // i=min(one.length(), length of array) |
502 if(one.length()<LENGTHOF(writeable)) { | 500 if(one.length()<UPRV_LENGTHOF(writeable)) { |
503 i=one.length(); | 501 i=one.length(); |
504 } else { | 502 } else { |
505 i=LENGTHOF(writeable); | 503 i=UPRV_LENGTHOF(writeable); |
506 } | 504 } |
507 one.extract(0, i, writeable); | 505 one.extract(0, i, writeable); |
508 for(i=0; i<LENGTHOF(writeable); ++i) { | 506 for(i=0; i<UPRV_LENGTHOF(writeable); ++i) { |
509 printf("writeable-alias backing buffer[%d]=0x%lx after re-extract\n", | 507 printf("writeable-alias backing buffer[%d]=0x%lx after re-extract\n", |
510 i, writeable[i]); | 508 i, writeable[i]); |
511 } | 509 } |
512 } | 510 } |
513 | 511 |
514 // sample code for UnicodeString instantiations ----------------------------- **
* | 512 // sample code for UnicodeString instantiations ----------------------------- **
* |
515 | 513 |
516 static void | 514 static void |
517 demoUnicodeStringInit() { | 515 demoUnicodeStringInit() { |
518 // *** Make sure to read about invariant characters in utypes.h! *** | 516 // *** Make sure to read about invariant characters in utypes.h! *** |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 printUnicodeString("german UnicodeString from unescaping:\n ", german); | 559 printUnicodeString("german UnicodeString from unescaping:\n ", german); |
562 | 560 |
563 /* | 561 /* |
564 * C: convert and unescape a char * string with only invariant | 562 * C: convert and unescape a char * string with only invariant |
565 * characters to fill a UChar * string | 563 * characters to fill a UChar * string |
566 */ | 564 */ |
567 UChar buffer[200]; | 565 UChar buffer[200]; |
568 int32_t length; | 566 int32_t length; |
569 length=u_unescape( | 567 length=u_unescape( |
570 "Sch\\u00f6nes Auto: \\u20ac 11240.\\fPrivates Zeichen: \\U00102345\\n", | 568 "Sch\\u00f6nes Auto: \\u20ac 11240.\\fPrivates Zeichen: \\U00102345\\n", |
571 buffer, LENGTHOF(buffer)); | 569 buffer, UPRV_LENGTHOF(buffer)); |
572 printf("german C Unicode string from char * unescaping: (length %d)\n ",
length); | 570 printf("german C Unicode string from char * unescaping: (length %d)\n ",
length); |
573 printUnicodeString("", UnicodeString(buffer)); | 571 printUnicodeString("", UnicodeString(buffer)); |
574 } | 572 } |
575 | 573 |
576 extern int | 574 extern int |
577 main(int argc, const char *argv[]) { | 575 main(int argc, const char *argv[]) { |
578 UErrorCode errorCode=U_ZERO_ERROR; | 576 UErrorCode errorCode=U_ZERO_ERROR; |
579 | 577 |
580 // Note: Using a global variable for any object is not exactly thread-safe..
. | 578 // Note: Using a global variable for any object is not exactly thread-safe..
. |
581 | 579 |
(...skipping 18 matching lines...) Expand all Loading... |
600 demo_utf_h_macros(); | 598 demo_utf_h_macros(); |
601 demo_C_Unicode_strings(); | 599 demo_C_Unicode_strings(); |
602 demoCaseMapInC(); | 600 demoCaseMapInC(); |
603 demoCaseMapInCPlusPlus(); | 601 demoCaseMapInCPlusPlus(); |
604 demoUnicodeStringStorage(); | 602 demoUnicodeStringStorage(); |
605 demoUnicodeStringInit(); | 603 demoUnicodeStringInit(); |
606 | 604 |
607 ucnv_close(cnv); | 605 ucnv_close(cnv); |
608 return 0; | 606 return 0; |
609 } | 607 } |
OLD | NEW |