]>
Commit | Line | Data |
---|---|---|
374ca955 A |
1 | /* |
2 | ******************************************************************************* | |
3 | * | |
46f4442e | 4 | * Copyright (C) 1999-2007, International Business Machines |
374ca955 A |
5 | * Corporation and others. All Rights Reserved. |
6 | * | |
7 | ******************************************************************************* | |
8 | * file name: unistr_props.cpp | |
9 | * encoding: US-ASCII | |
10 | * tab size: 8 (not used) | |
11 | * indentation:2 | |
12 | * | |
13 | * created on: 2004aug25 | |
14 | * created by: Markus W. Scherer | |
15 | * | |
16 | * Character property dependent functions moved here from unistr.cpp | |
17 | */ | |
18 | ||
19 | #include "unicode/utypes.h" | |
20 | #include "unicode/uchar.h" | |
21 | #include "unicode/unistr.h" | |
22 | ||
46f4442e A |
23 | U_NAMESPACE_BEGIN |
24 | ||
374ca955 A |
25 | UnicodeString& |
26 | UnicodeString::trim() | |
27 | { | |
28 | if(isBogus()) { | |
29 | return *this; | |
30 | } | |
31 | ||
46f4442e | 32 | UChar *array = getArrayStart(); |
374ca955 | 33 | UChar32 c; |
46f4442e A |
34 | int32_t oldLength = this->length(); |
35 | int32_t i = oldLength, length; | |
374ca955 A |
36 | |
37 | // first cut off trailing white space | |
38 | for(;;) { | |
39 | length = i; | |
40 | if(i <= 0) { | |
41 | break; | |
42 | } | |
46f4442e | 43 | U16_PREV(array, 0, i, c); |
374ca955 A |
44 | if(!(c == 0x20 || u_isWhitespace(c))) { |
45 | break; | |
46 | } | |
47 | } | |
46f4442e A |
48 | if(length < oldLength) { |
49 | setLength(length); | |
374ca955 A |
50 | } |
51 | ||
52 | // find leading white space | |
53 | int32_t start; | |
54 | i = 0; | |
55 | for(;;) { | |
56 | start = i; | |
57 | if(i >= length) { | |
58 | break; | |
59 | } | |
46f4442e | 60 | U16_NEXT(array, i, length, c); |
374ca955 A |
61 | if(!(c == 0x20 || u_isWhitespace(c))) { |
62 | break; | |
63 | } | |
64 | } | |
65 | ||
66 | // move string forward over leading white space | |
67 | if(start > 0) { | |
68 | doReplace(0, start, 0, 0, 0); | |
69 | } | |
70 | ||
71 | return *this; | |
72 | } | |
46f4442e A |
73 | |
74 | U_NAMESPACE_END |