00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifndef __ZMM_STRINGS_H__
00033 #define __ZMM_STRINGS_H__
00034
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include <unistd.h>
00039
00040 #include "object.h"
00041 #include "ref.h"
00042
00043 #define _(str) zmm::String::refer(str)
00044
00045
00046 #define MAX_INT_STRING_LENGTH 12
00047
00048
00049 #define MAX_LONG_STRING_LENGTH 22
00050
00051 #define MAX_DOUBLE_STRING_LENGTH 24
00052
00053 #define MAX_LONG_LONG_STRING_LENGTH 24
00054
00055 #define MAX_INT64_T_STRING_LENGTH 24
00056
00057 namespace zmm
00058 {
00059
00060 class StringBase : public Object
00061 {
00062 public:
00063 char *data;
00064 int len;
00065 bool store;
00066
00067 StringBase(int capacity);
00068 StringBase(const char *str);
00069 StringBase(const char *str, int len);
00070 bool startsWith(StringBase *other);
00071 virtual ~StringBase();
00072 protected:
00073 inline StringBase() : Object() {}
00074 friend class String;
00075 };
00076
00077
00078 class StringBuffer;
00079
00080
00081 class String
00082 {
00083 protected:
00084 StringBase *base;
00085 public:
00086 String();
00087 String(const char *str);
00088 explicit String(char ch);
00089 String(const char *str, int len);
00090 String(const String &other);
00091 String(StringBase *other);
00092 String(Ref<StringBase> other);
00093
00094 inline StringBase *getBase()
00095 {
00096 return base;
00097 }
00098
00099 inline String(NIL_VAR)
00100 {
00101 base = NULL;
00102 }
00103
00104 ~String();
00105
00106 String &operator=(const char *str);
00107
00108 String &operator=(String other);
00109
00110 inline String &operator=(NIL_VAR)
00111 {
00112 if(base)
00113 base->release();
00114 base = NULL;
00115 return *this;
00116 }
00117
00118 String operator+(String other);
00119 String operator+(const char *str);
00120 String operator+(char chr);
00121 String operator+(int x);
00122 String operator+(unsigned int x);
00123 String operator+(double x);
00124
00125 int operator==(String other);
00126 int operator==(const char *str);
00127 int operator==(char c);
00128
00129 inline int operator!=(String other)
00130 {
00131 return ! operator==(other);
00132 }
00133 inline int operator!=(const char *str)
00134 {
00135 return ! operator==(str);
00136 }
00137 inline int operator!=(char c)
00138 {
00139 return ! operator==(c);
00140 }
00141
00142 inline int operator==(NIL_VAR)
00143 {
00144 return (base == NULL);
00145 }
00146 inline int operator!=(NIL_VAR)
00147 {
00148 return (base != NULL);
00149 }
00150
00151
00152 inline operator Ref<StringBase>()
00153 {
00154 return Ref<StringBase>(base);
00155 }
00156
00157 int equals(String other, bool ignoreCase = false);
00158 String toLower();
00159 String toUpper();
00160
00161 String substring(int from);
00162 String substring(int from, int count);
00163
00168 String reduce(char ch);
00169
00170 inline char charAt(int index) { return base->data[index]; }
00171 inline char *charPtrAt(int index) { return base->data + index; }
00172 inline int index(char ch) { return index(0, ch); }
00173 int index(int start, char ch);
00174 int rindex(char ch);
00175 int rindex(int end, char ch);
00176
00177 long toLong();
00178 off_t toOFF_T();
00179 inline int toInt() { return (int)toLong(); }
00180 inline unsigned int toUInt() { return (unsigned int)toLong(); }
00181 double toDouble();
00182
00183 int length();
00184 inline void setLength(int length)
00185 {
00186 base->len = length;
00187 }
00188 char *c_str();
00189 inline void updateLength()
00190 {
00191 base->len = strlen(base->data);
00192 }
00193
00194 bool startsWith(String str)
00195 {
00196 return base->startsWith(str.base);
00197 }
00198
00199 int find(const char *needle);
00200 int find(String needle);
00201 String replace(String needle, String replacement);
00202 String replaceChar(char needle , char replacement);
00203
00204 static String from(int x);
00205 static String from(unsigned int x);
00206 static String from(long x);
00207 static String from(unsigned long x);
00208 static String from(double x);
00209 static String from(long long x);
00210
00211 static String allocate(int size);
00212 static String take(const char *data, int length);
00213 static String take(const char *data);
00214 static String refer(const char *str);
00215 static String refer(const char *str, int len);
00216 static String copy(const char *str);
00217 protected:
00218 String(int capacity);
00219 friend class StringBuffer;
00220 };
00221
00222 };
00223
00224 #endif // __STRINGS_H__