1/*
2*******************************************************************************
3*
4*   Copyright (C) 1999-2007, International Business Machines
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
23U_NAMESPACE_BEGIN
24
25UnicodeString&
26UnicodeString::trim()
27{
28  if(isBogus()) {
29    return *this;
30  }
31
32  UChar *array = getArrayStart();
33  UChar32 c;
34  int32_t oldLength = this->length();
35  int32_t i = oldLength, length;
36
37  // first cut off trailing white space
38  for(;;) {
39    length = i;
40    if(i <= 0) {
41      break;
42    }
43    U16_PREV(array, 0, i, c);
44    if(!(c == 0x20 || u_isWhitespace(c))) {
45      break;
46    }
47  }
48  if(length < oldLength) {
49    setLength(length);
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    }
60    U16_NEXT(array, i, length, c);
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}
73
74U_NAMESPACE_END
75