unistr_props.cpp 1.6 KB

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