charstr.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (c) 2001-2015, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. * Date Name Description
  9. * 11/19/2001 aliu Creation.
  10. * 05/19/2010 markus Rewritten from scratch
  11. **********************************************************************
  12. */
  13. #ifndef CHARSTRING_H
  14. #define CHARSTRING_H
  15. #include "unicode/utypes.h"
  16. #include "unicode/unistr.h"
  17. #include "unicode/uobject.h"
  18. #include "cmemory.h"
  19. U_NAMESPACE_BEGIN
  20. // Windows needs us to DLL-export the MaybeStackArray template specialization,
  21. // but MacOS X cannot handle it. Same as in digitlst.h.
  22. #if !U_PLATFORM_IS_DARWIN_BASED
  23. template class U_COMMON_API MaybeStackArray<char, 40>;
  24. #endif
  25. /**
  26. * ICU-internal char * string class.
  27. * This class does not assume or enforce any particular character encoding.
  28. * Raw bytes can be stored. The string object owns its characters.
  29. * A terminating NUL is stored, but the class does not prevent embedded NUL characters.
  30. *
  31. * This class wants to be convenient but is also deliberately minimalist.
  32. * Please do not add methods if they only add minor convenience.
  33. * For example:
  34. * cs.data()[5]='a'; // no need for setCharAt(5, 'a')
  35. */
  36. class U_COMMON_API CharString : public UMemory {
  37. public:
  38. CharString() : len(0) { buffer[0]=0; }
  39. CharString(StringPiece s, UErrorCode &errorCode) : len(0) {
  40. buffer[0]=0;
  41. append(s, errorCode);
  42. }
  43. CharString(const CharString &s, UErrorCode &errorCode) : len(0) {
  44. buffer[0]=0;
  45. append(s, errorCode);
  46. }
  47. CharString(const char *s, int32_t sLength, UErrorCode &errorCode) : len(0) {
  48. buffer[0]=0;
  49. append(s, sLength, errorCode);
  50. }
  51. ~CharString() {}
  52. /**
  53. * Move constructor; might leave src in an undefined state.
  54. * This string will have the same contents and state that the source string had.
  55. */
  56. CharString(CharString &&src) noexcept;
  57. /**
  58. * Move assignment operator; might leave src in an undefined state.
  59. * This string will have the same contents and state that the source string had.
  60. * The behavior is undefined if *this and src are the same object.
  61. */
  62. CharString &operator=(CharString &&src) noexcept;
  63. /**
  64. * Replaces this string's contents with the other string's contents.
  65. * CharString does not support the standard copy constructor nor
  66. * the assignment operator, to make copies explicit and to
  67. * use a UErrorCode where memory allocations might be needed.
  68. */
  69. CharString &copyFrom(const CharString &other, UErrorCode &errorCode);
  70. UBool isEmpty() const { return len==0; }
  71. int32_t length() const { return len; }
  72. char operator[](int32_t index) const { return buffer[index]; }
  73. StringPiece toStringPiece() const { return StringPiece(buffer.getAlias(), len); }
  74. const char *data() const { return buffer.getAlias(); }
  75. char *data() { return buffer.getAlias(); }
  76. /**
  77. * Allocates length()+1 chars and copies the NUL-terminated data().
  78. * The caller must uprv_free() the result.
  79. */
  80. char *cloneData(UErrorCode &errorCode) const;
  81. /**
  82. * Copies the contents of the string into dest.
  83. * Checks if there is enough space in dest, extracts the entire string if possible,
  84. * and NUL-terminates dest if possible.
  85. *
  86. * If the string fits into dest but cannot be NUL-terminated (length()==capacity),
  87. * then the error code is set to U_STRING_NOT_TERMINATED_WARNING.
  88. * If the string itself does not fit into dest (length()>capacity),
  89. * then the error code is set to U_BUFFER_OVERFLOW_ERROR.
  90. *
  91. * @param dest Destination string buffer.
  92. * @param capacity Size of the dest buffer (number of chars).
  93. * @param errorCode ICU error code.
  94. * @return length()
  95. */
  96. int32_t extract(char *dest, int32_t capacity, UErrorCode &errorCode) const;
  97. bool operator==(StringPiece other) const {
  98. return len == other.length() && (len == 0 || uprv_memcmp(data(), other.data(), len) == 0);
  99. }
  100. bool operator!=(StringPiece other) const {
  101. return !operator==(other);
  102. }
  103. /** @return last index of c, or -1 if c is not in this string */
  104. int32_t lastIndexOf(char c) const;
  105. bool contains(StringPiece s) const;
  106. CharString &clear() { len=0; buffer[0]=0; return *this; }
  107. CharString &truncate(int32_t newLength);
  108. CharString &append(char c, UErrorCode &errorCode);
  109. CharString &append(StringPiece s, UErrorCode &errorCode) {
  110. return append(s.data(), s.length(), errorCode);
  111. }
  112. CharString &append(const CharString &s, UErrorCode &errorCode) {
  113. return append(s.data(), s.length(), errorCode);
  114. }
  115. CharString &append(const char *s, int32_t sLength, UErrorCode &status);
  116. CharString &appendNumber(int32_t number, UErrorCode &status);
  117. /**
  118. * Returns a writable buffer for appending and writes the buffer's capacity to
  119. * resultCapacity. Guarantees resultCapacity>=minCapacity if U_SUCCESS().
  120. * There will additionally be space for a terminating NUL right at resultCapacity.
  121. * (This function is similar to ByteSink.GetAppendBuffer().)
  122. *
  123. * The returned buffer is only valid until the next write operation
  124. * on this string.
  125. *
  126. * After writing at most resultCapacity bytes, call append() with the
  127. * pointer returned from this function and the number of bytes written.
  128. *
  129. * @param minCapacity required minimum capacity of the returned buffer;
  130. * must be non-negative
  131. * @param desiredCapacityHint desired capacity of the returned buffer;
  132. * must be non-negative
  133. * @param resultCapacity will be set to the capacity of the returned buffer
  134. * @param errorCode in/out error code
  135. * @return a buffer with resultCapacity>=min_capacity
  136. */
  137. char *getAppendBuffer(int32_t minCapacity,
  138. int32_t desiredCapacityHint,
  139. int32_t &resultCapacity,
  140. UErrorCode &errorCode);
  141. CharString &appendInvariantChars(const UnicodeString &s, UErrorCode &errorCode);
  142. CharString &appendInvariantChars(const char16_t* uchars, int32_t ucharsLen, UErrorCode& errorCode);
  143. /**
  144. * Appends a filename/path part, e.g., a directory name.
  145. * First appends a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR if necessary.
  146. * Does nothing if s is empty.
  147. */
  148. CharString &appendPathPart(StringPiece s, UErrorCode &errorCode);
  149. /**
  150. * Appends a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR if this string is not empty
  151. * and does not already end with a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR.
  152. */
  153. CharString &ensureEndsWithFileSeparator(UErrorCode &errorCode);
  154. private:
  155. MaybeStackArray<char, 40> buffer;
  156. int32_t len;
  157. UBool ensureCapacity(int32_t capacity, int32_t desiredCapacityHint, UErrorCode &errorCode);
  158. CharString(const CharString &other) = delete; // forbid copying of this class
  159. CharString &operator=(const CharString &other) = delete; // forbid copying of this class
  160. /**
  161. * Returns U_FILE_ALT_SEP_CHAR if found in string, and U_FILE_SEP_CHAR is not found.
  162. * Otherwise returns U_FILE_SEP_CHAR.
  163. */
  164. char getDirSepChar() const;
  165. };
  166. U_NAMESPACE_END
  167. #endif
  168. //eof