media/omx-plugin/include/ics/utils/String8.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /*
     2  * Copyright (C) 2005 The Android Open Source Project
     3  *
     4  * Licensed under the Apache License, Version 2.0 (the "License");
     5  * you may not use this file except in compliance with the License.
     6  * You may obtain a copy of the License at
     7  *
     8  *      http://www.apache.org/licenses/LICENSE-2.0
     9  *
    10  * Unless required by applicable law or agreed to in writing, software
    11  * distributed under the License is distributed on an "AS IS" BASIS,
    12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  * See the License for the specific language governing permissions and
    14  * limitations under the License.
    15  */
    17 #ifndef ANDROID_STRING8_H
    18 #define ANDROID_STRING8_H
    20 #include <utils/Errors.h>
    21 #include <utils/SharedBuffer.h>
    22 #include <utils/Unicode.h>
    24 #include <string.h> // for strcmp
    25 #include <stdarg.h>
    27 // ---------------------------------------------------------------------------
    29 namespace android {
    31 class String16;
    32 class TextOutput;
    34 //! This is a string holding UTF-8 characters. Does not allow the value more
    35 // than 0x10FFFF, which is not valid unicode codepoint.
    36 class String8
    37 {
    38 public:
    39                                 String8();
    40                                 String8(const String8& o);
    41     explicit                    String8(const char* o);
    42     explicit                    String8(const char* o, size_t numChars);
    44     explicit                    String8(const String16& o);
    45     explicit                    String8(const char16_t* o);
    46     explicit                    String8(const char16_t* o, size_t numChars);
    47     explicit                    String8(const char32_t* o);
    48     explicit                    String8(const char32_t* o, size_t numChars);
    49                                 ~String8();
    51     static inline const String8 empty();
    53     static String8              format(const char* fmt, ...) __attribute__((format (printf, 1, 2)));
    54     static String8              formatV(const char* fmt, va_list args);
    56     inline  const char*         string() const;
    57     inline  size_t              size() const;
    58     inline  size_t              length() const;
    59     inline  size_t              bytes() const;
    60     inline  bool                isEmpty() const;
    62     inline  const SharedBuffer* sharedBuffer() const;
    64             void                clear();
    66             void                setTo(const String8& other);
    67             status_t            setTo(const char* other);
    68             status_t            setTo(const char* other, size_t numChars);
    69             status_t            setTo(const char16_t* other, size_t numChars);
    70             status_t            setTo(const char32_t* other,
    71                                       size_t length);
    73             status_t            append(const String8& other);
    74             status_t            append(const char* other);
    75             status_t            append(const char* other, size_t numChars);
    77             status_t            appendFormat(const char* fmt, ...)
    78                     __attribute__((format (printf, 2, 3)));
    79             status_t            appendFormatV(const char* fmt, va_list args);
    81             // Note that this function takes O(N) time to calculate the value.
    82             // No cache value is stored.
    83             size_t              getUtf32Length() const;
    84             int32_t             getUtf32At(size_t index,
    85                                            size_t *next_index) const;
    86             void                getUtf32(char32_t* dst) const;
    88     inline  String8&            operator=(const String8& other);
    89     inline  String8&            operator=(const char* other);
    91     inline  String8&            operator+=(const String8& other);
    92     inline  String8             operator+(const String8& other) const;
    94     inline  String8&            operator+=(const char* other);
    95     inline  String8             operator+(const char* other) const;
    97     inline  int                 compare(const String8& other) const;
    99     inline  bool                operator<(const String8& other) const;
   100     inline  bool                operator<=(const String8& other) const;
   101     inline  bool                operator==(const String8& other) const;
   102     inline  bool                operator!=(const String8& other) const;
   103     inline  bool                operator>=(const String8& other) const;
   104     inline  bool                operator>(const String8& other) const;
   106     inline  bool                operator<(const char* other) const;
   107     inline  bool                operator<=(const char* other) const;
   108     inline  bool                operator==(const char* other) const;
   109     inline  bool                operator!=(const char* other) const;
   110     inline  bool                operator>=(const char* other) const;
   111     inline  bool                operator>(const char* other) const;
   113     inline                      operator const char*() const;
   115             char*               lockBuffer(size_t size);
   116             void                unlockBuffer();
   117             status_t            unlockBuffer(size_t size);
   119             // return the index of the first byte of other in this at or after
   120             // start, or -1 if not found
   121             ssize_t             find(const char* other, size_t start = 0) const;
   123             void                toLower();
   124             void                toLower(size_t start, size_t numChars);
   125             void                toUpper();
   126             void                toUpper(size_t start, size_t numChars);
   128     /*
   129      * These methods operate on the string as if it were a path name.
   130      */
   132     /*
   133      * Set the filename field to a specific value.
   134      *
   135      * Normalizes the filename, removing a trailing '/' if present.
   136      */
   137     void setPathName(const char* name);
   138     void setPathName(const char* name, size_t numChars);
   140     /*
   141      * Get just the filename component.
   142      *
   143      * "/tmp/foo/bar.c" --> "bar.c"
   144      */
   145     String8 getPathLeaf(void) const;
   147     /*
   148      * Remove the last (file name) component, leaving just the directory
   149      * name.
   150      *
   151      * "/tmp/foo/bar.c" --> "/tmp/foo"
   152      * "/tmp" --> "" // ????? shouldn't this be "/" ???? XXX
   153      * "bar.c" --> ""
   154      */
   155     String8 getPathDir(void) const;
   157     /*
   158      * Retrieve the front (root dir) component.  Optionally also return the
   159      * remaining components.
   160      *
   161      * "/tmp/foo/bar.c" --> "tmp" (remain = "foo/bar.c")
   162      * "/tmp" --> "tmp" (remain = "")
   163      * "bar.c" --> "bar.c" (remain = "")
   164      */
   165     String8 walkPath(String8* outRemains = NULL) const;
   167     /*
   168      * Return the filename extension.  This is the last '.' and any number
   169      * of characters that follow it.  The '.' is included in case we
   170      * decide to expand our definition of what constitutes an extension.
   171      *
   172      * "/tmp/foo/bar.c" --> ".c"
   173      * "/tmp" --> ""
   174      * "/tmp/foo.bar/baz" --> ""
   175      * "foo.jpeg" --> ".jpeg"
   176      * "foo." --> ""
   177      */
   178     String8 getPathExtension(void) const;
   180     /*
   181      * Return the path without the extension.  Rules for what constitutes
   182      * an extension are described in the comment for getPathExtension().
   183      *
   184      * "/tmp/foo/bar.c" --> "/tmp/foo/bar"
   185      */
   186     String8 getBasePath(void) const;
   188     /*
   189      * Add a component to the pathname.  We guarantee that there is
   190      * exactly one path separator between the old path and the new.
   191      * If there is no existing name, we just copy the new name in.
   192      *
   193      * If leaf is a fully qualified path (i.e. starts with '/', it
   194      * replaces whatever was there before.
   195      */
   196     String8& appendPath(const char* leaf);
   197     String8& appendPath(const String8& leaf)  { return appendPath(leaf.string()); }
   199     /*
   200      * Like appendPath(), but does not affect this string.  Returns a new one instead.
   201      */
   202     String8 appendPathCopy(const char* leaf) const
   203                                              { String8 p(*this); p.appendPath(leaf); return p; }
   204     String8 appendPathCopy(const String8& leaf) const { return appendPathCopy(leaf.string()); }
   206     /*
   207      * Converts all separators in this string to /, the default path separator.
   208      *
   209      * If the default OS separator is backslash, this converts all
   210      * backslashes to slashes, in-place. Otherwise it does nothing.
   211      * Returns self.
   212      */
   213     String8& convertToResPath();
   215 private:
   216             status_t            real_append(const char* other, size_t numChars);
   217             char*               find_extension(void) const;
   219             const char* mString;
   220 };
   222 TextOutput& operator<<(TextOutput& to, const String16& val);
   224 // ---------------------------------------------------------------------------
   225 // No user servicable parts below.
   227 inline int compare_type(const String8& lhs, const String8& rhs)
   228 {
   229     return lhs.compare(rhs);
   230 }
   232 inline int strictly_order_type(const String8& lhs, const String8& rhs)
   233 {
   234     return compare_type(lhs, rhs) < 0;
   235 }
   237 inline const String8 String8::empty() {
   238     return String8();
   239 }
   241 inline const char* String8::string() const
   242 {
   243     return mString;
   244 }
   246 inline size_t String8::length() const
   247 {
   248     return SharedBuffer::sizeFromData(mString)-1;
   249 }
   251 inline size_t String8::size() const
   252 {
   253     return length();
   254 }
   256 inline bool String8::isEmpty() const
   257 {
   258     return length() == 0;
   259 }
   261 inline size_t String8::bytes() const
   262 {
   263     return SharedBuffer::sizeFromData(mString)-1;
   264 }
   266 inline const SharedBuffer* String8::sharedBuffer() const
   267 {
   268     return SharedBuffer::bufferFromData(mString);
   269 }
   271 inline String8& String8::operator=(const String8& other)
   272 {
   273     setTo(other);
   274     return *this;
   275 }
   277 inline String8& String8::operator=(const char* other)
   278 {
   279     setTo(other);
   280     return *this;
   281 }
   283 inline String8& String8::operator+=(const String8& other)
   284 {
   285     append(other);
   286     return *this;
   287 }
   289 inline String8 String8::operator+(const String8& other) const
   290 {
   291     String8 tmp(*this);
   292     tmp += other;
   293     return tmp;
   294 }
   296 inline String8& String8::operator+=(const char* other)
   297 {
   298     append(other);
   299     return *this;
   300 }
   302 inline String8 String8::operator+(const char* other) const
   303 {
   304     String8 tmp(*this);
   305     tmp += other;
   306     return tmp;
   307 }
   309 inline int String8::compare(const String8& other) const
   310 {
   311     return strcmp(mString, other.mString);
   312 }
   314 inline bool String8::operator<(const String8& other) const
   315 {
   316     return strcmp(mString, other.mString) < 0;
   317 }
   319 inline bool String8::operator<=(const String8& other) const
   320 {
   321     return strcmp(mString, other.mString) <= 0;
   322 }
   324 inline bool String8::operator==(const String8& other) const
   325 {
   326     return strcmp(mString, other.mString) == 0;
   327 }
   329 inline bool String8::operator!=(const String8& other) const
   330 {
   331     return strcmp(mString, other.mString) != 0;
   332 }
   334 inline bool String8::operator>=(const String8& other) const
   335 {
   336     return strcmp(mString, other.mString) >= 0;
   337 }
   339 inline bool String8::operator>(const String8& other) const
   340 {
   341     return strcmp(mString, other.mString) > 0;
   342 }
   344 inline bool String8::operator<(const char* other) const
   345 {
   346     return strcmp(mString, other) < 0;
   347 }
   349 inline bool String8::operator<=(const char* other) const
   350 {
   351     return strcmp(mString, other) <= 0;
   352 }
   354 inline bool String8::operator==(const char* other) const
   355 {
   356     return strcmp(mString, other) == 0;
   357 }
   359 inline bool String8::operator!=(const char* other) const
   360 {
   361     return strcmp(mString, other) != 0;
   362 }
   364 inline bool String8::operator>=(const char* other) const
   365 {
   366     return strcmp(mString, other) >= 0;
   367 }
   369 inline bool String8::operator>(const char* other) const
   370 {
   371     return strcmp(mString, other) > 0;
   372 }
   374 inline String8::operator const char*() const
   375 {
   376     return mString;
   377 }
   379 }  // namespace android
   381 // ---------------------------------------------------------------------------
   383 #endif // ANDROID_STRING8_H

mercurial