string_segment.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // © 2018 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. #include "unicode/utypes.h"
  4. #if !UCONFIG_NO_FORMATTING
  5. #ifndef __NUMPARSE_STRINGSEGMENT_H__
  6. #define __NUMPARSE_STRINGSEGMENT_H__
  7. #include "unicode/unistr.h"
  8. #include "unicode/uniset.h"
  9. U_NAMESPACE_BEGIN
  10. /**
  11. * A mutable UnicodeString wrapper with a variable offset and length and
  12. * support for case folding. The charAt, length, and subSequence methods all
  13. * operate relative to the fixed offset into the UnicodeString.
  14. *
  15. * Intended to be useful for parsing.
  16. *
  17. * CAUTION: Since this class is mutable, it must not be used anywhere that an
  18. * immutable object is required, like in a cache or as the key of a hash map.
  19. *
  20. * @author sffc (Shane Carr)
  21. */
  22. // Exported as U_I18N_API for tests
  23. class U_I18N_API StringSegment : public UMemory {
  24. public:
  25. StringSegment(const UnicodeString& str, bool ignoreCase);
  26. int32_t getOffset() const;
  27. void setOffset(int32_t start);
  28. /**
  29. * Equivalent to <code>setOffset(getOffset()+delta)</code>.
  30. *
  31. * <p>
  32. * This method is usually called by a Matcher to register that a char was consumed. If the char is
  33. * strong (it usually is, except for things like whitespace), follow this with a call to
  34. * {@link ParsedNumber#setCharsConsumed}. For more information on strong chars, see that method.
  35. */
  36. void adjustOffset(int32_t delta);
  37. /**
  38. * Adjusts the offset by the width of the current code point, either 1 or 2 chars.
  39. */
  40. void adjustOffsetByCodePoint();
  41. void setLength(int32_t length);
  42. void resetLength();
  43. int32_t length() const;
  44. char16_t charAt(int32_t index) const;
  45. UChar32 codePointAt(int32_t index) const;
  46. UnicodeString toUnicodeString() const;
  47. const UnicodeString toTempUnicodeString() const;
  48. /**
  49. * Returns the first code point in the string segment, or -1 if the string starts with an invalid
  50. * code point.
  51. *
  52. * <p>
  53. * <strong>Important:</strong> Most of the time, you should use {@link #startsWith}, which handles case
  54. * folding logic, instead of this method.
  55. */
  56. UChar32 getCodePoint() const;
  57. /**
  58. * Returns true if the first code point of this StringSegment equals the given code point.
  59. *
  60. * <p>
  61. * This method will perform case folding if case folding is enabled for the parser.
  62. */
  63. bool startsWith(UChar32 otherCp) const;
  64. /**
  65. * Returns true if the first code point of this StringSegment is in the given UnicodeSet.
  66. */
  67. bool startsWith(const UnicodeSet& uniset) const;
  68. /**
  69. * Returns true if there is at least one code point of overlap between this StringSegment and the
  70. * given UnicodeString.
  71. */
  72. bool startsWith(const UnicodeString& other) const;
  73. /**
  74. * Returns the length of the prefix shared by this StringSegment and the given UnicodeString. For
  75. * example, if this string segment is "aab", and the char sequence is "aac", this method returns 2,
  76. * since the first 2 characters are the same.
  77. *
  78. * <p>
  79. * This method only returns offsets along code point boundaries.
  80. *
  81. * <p>
  82. * This method will perform case folding if case folding was enabled in the constructor.
  83. *
  84. * <p>
  85. * IMPORTANT: The given UnicodeString must not be empty! It is the caller's responsibility to check.
  86. */
  87. int32_t getCommonPrefixLength(const UnicodeString& other);
  88. /**
  89. * Like {@link #getCommonPrefixLength}, but never performs case folding, even if case folding is
  90. * enabled for the parser.
  91. */
  92. int32_t getCaseSensitivePrefixLength(const UnicodeString& other);
  93. bool operator==(const UnicodeString& other) const;
  94. private:
  95. const UnicodeString& fStr;
  96. int32_t fStart;
  97. int32_t fEnd;
  98. bool fFoldCase;
  99. int32_t getPrefixLengthInternal(const UnicodeString& other, bool foldCase);
  100. static bool codePointsEqual(UChar32 cp1, UChar32 cp2, bool foldCase);
  101. };
  102. U_NAMESPACE_END
  103. #endif //__NUMPARSE_STRINGSEGMENT_H__
  104. #endif /* #if !UCONFIG_NO_FORMATTING */