]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/common/charstr.h
ICU-66108.tar.gz
[apple/icu.git] / icuSources / common / charstr.h
index b655361527913cb7fdc05c30b8262f20d893d2af..23b950ed6ecc769f275e3018406a2ace87936eb6 100644 (file)
@@ -1,6 +1,8 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
 **********************************************************************
-*   Copyright (c) 2001-2011, International Business Machines
+*   Copyright (c) 2001-2015, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 **********************************************************************
 *   Date        Name        Description
@@ -39,7 +41,7 @@ template class U_COMMON_API MaybeStackArray<char, 40>;
 class U_COMMON_API CharString : public UMemory {
 public:
     CharString() : len(0) { buffer[0]=0; }
-    CharString(const StringPiece &s, UErrorCode &errorCode) : len(0) {
+    CharString(StringPiece s, UErrorCode &errorCode) : len(0) {
         buffer[0]=0;
         append(s, errorCode);
     }
@@ -53,6 +55,18 @@ public:
     }
     ~CharString() {}
 
+    /**
+     * Move constructor; might leave src in an undefined state.
+     * This string will have the same contents and state that the source string had.
+     */
+    CharString(CharString &&src) U_NOEXCEPT;
+    /**
+     * Move assignment operator; might leave src in an undefined state.
+     * This string will have the same contents and state that the source string had.
+     * The behavior is undefined if *this and src are the same object.
+     */
+    CharString &operator=(CharString &&src) U_NOEXCEPT;
+
     /**
      * Replaces this string's contents with the other string's contents.
      * CharString does not support the standard copy constructor nor
@@ -61,19 +75,36 @@ public:
      */
     CharString &copyFrom(const CharString &other, UErrorCode &errorCode);
 
-    UBool isEmpty() { return len==0; }
+    UBool isEmpty() const { return len==0; }
     int32_t length() const { return len; }
-    char operator[] (int32_t index) const { return buffer[index]; }
+    char operator[](int32_t index) const { return buffer[index]; }
     StringPiece toStringPiece() const { return StringPiece(buffer.getAlias(), len); }
 
     const char *data() const { return buffer.getAlias(); }
     char *data() { return buffer.getAlias(); }
+    /**
+     * Allocates length()+1 chars and copies the NUL-terminated data().
+     * The caller must uprv_free() the result.
+     */
+    char *cloneData(UErrorCode &errorCode) const;
+
+    bool operator==(StringPiece other) const {
+        return len == other.length() && (len == 0 || uprv_memcmp(data(), other.data(), len) == 0);
+    }
+    bool operator!=(StringPiece other) const {
+        return !operator==(other);
+    }
+
+    /** @return last index of c, or -1 if c is not in this string */
+    int32_t lastIndexOf(char c) const;
+
+    bool contains(StringPiece s) const;
 
     CharString &clear() { len=0; buffer[0]=0; return *this; }
     CharString &truncate(int32_t newLength);
 
     CharString &append(char c, UErrorCode &errorCode);
-    CharString &append(const StringPiece &s, UErrorCode &errorCode) {
+    CharString &append(StringPiece s, UErrorCode &errorCode) {
         return append(s.data(), s.length(), errorCode);
     }
     CharString &append(const CharString &s, UErrorCode &errorCode) {
@@ -106,13 +137,20 @@ public:
                           UErrorCode &errorCode);
 
     CharString &appendInvariantChars(const UnicodeString &s, UErrorCode &errorCode);
+    CharString &appendInvariantChars(const UChar* uchars, int32_t ucharsLen, UErrorCode& errorCode);
 
     /**
      * Appends a filename/path part, e.g., a directory name.
      * First appends a U_FILE_SEP_CHAR if necessary.
      * Does nothing if s is empty.
      */
-    CharString &appendPathPart(const StringPiece &s, UErrorCode &errorCode);
+    CharString &appendPathPart(StringPiece s, UErrorCode &errorCode);
+
+    /**
+     * Appends a U_FILE_SEP_CHAR if this string is not empty
+     * and does not already end with a U_FILE_SEP_CHAR or U_FILE_ALT_SEP_CHAR.
+     */
+    CharString &ensureEndsWithFileSeparator(UErrorCode &errorCode);
 
 private:
     MaybeStackArray<char, 40> buffer;