Home | History | Annotate | Download | only in foundation
      1 /*
      2  * Copyright (C) 2010 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  */
     16 
     17 #ifndef A_STRING_H_
     18 
     19 #define A_STRING_H_
     20 
     21 #include <sys/types.h>
     22 
     23 namespace android {
     24 
     25 struct AString {
     26     AString();
     27     AString(const char *s);
     28     AString(const char *s, size_t size);
     29     AString(const AString &from);
     30     AString(const AString &from, size_t offset, size_t n);
     31     ~AString();
     32 
     33     AString &operator=(const AString &from);
     34     void setTo(const char *s);
     35     void setTo(const char *s, size_t size);
     36     void setTo(const AString &from, size_t offset, size_t n);
     37 
     38     size_t size() const;
     39     const char *c_str() const;
     40 
     41     bool empty() const;
     42 
     43     void clear();
     44     void trim();
     45     void erase(size_t start, size_t n);
     46 
     47     void append(char c) { append(&c, 1); }
     48     void append(const char *s);
     49     void append(const char *s, size_t size);
     50     void append(const AString &from);
     51     void append(const AString &from, size_t offset, size_t n);
     52     void append(int x);
     53     void append(unsigned x);
     54     void append(long x);
     55     void append(unsigned long x);
     56     void append(long long x);
     57     void append(unsigned long long x);
     58     void append(float x);
     59     void append(double x);
     60     void append(void *x);
     61 
     62     void insert(const AString &from, size_t insertionPos);
     63     void insert(const char *from, size_t size, size_t insertionPos);
     64 
     65     ssize_t find(const char *substring, size_t start = 0) const;
     66 
     67     size_t hash() const;
     68 
     69     bool operator==(const AString &other) const;
     70     bool operator<(const AString &other) const;
     71     bool operator>(const AString &other) const;
     72 
     73     int compare(const AString &other) const;
     74 
     75     bool startsWith(const char *prefix) const;
     76     bool endsWith(const char *suffix) const;
     77 
     78     void tolower();
     79 
     80 private:
     81     static const char *kEmptyString;
     82 
     83     char *mData;
     84     size_t mSize;
     85     size_t mAllocSize;
     86 
     87     void makeMutable();
     88 };
     89 
     90 AString StringPrintf(const char *format, ...);
     91 
     92 }  // namespace android
     93 
     94 #endif  // A_STRING_H_
     95 
     96