foobar2000 SDK  2015-08-03
stringNew.h
Go to the documentation of this file.
1 namespace pfc {
2  //helper, const methods only
3  class __stringEmpty : public string_base {
4  public:
5  const char * get_ptr() const {return "";}
6  void add_string(const char * p_string,t_size p_length = ~0) {throw exception_not_implemented();}
7  void set_string(const char * p_string,t_size p_length = ~0) {throw exception_not_implemented();}
8  void truncate(t_size len) {throw exception_not_implemented();}
9  t_size get_length() const {return 0;}
10  char * lock_buffer(t_size p_requested_length) {throw exception_not_implemented();}
11  void unlock_buffer() {throw exception_not_implemented();}
12  };
13 
14  class stringp;
15 
19  class string {
20  public:
23 
24  string() : m_content(rcnew_t<__stringEmpty>()) {}
25  string(const char * p_source) : m_content(rcnew_t<string8>(p_source)) {}
26  string(const char * p_source, t_size p_sourceLen) : m_content(rcnew_t<string8>(p_source,p_sourceLen)) {}
27  string(char * p_source) : m_content(rcnew_t<string8>(p_source)) {}
28  string(char * p_source, t_size p_sourceLen) : m_content(rcnew_t<string8>(p_source,p_sourceLen)) {}
29  string(t_data const & p_source) : m_content(p_source) {}
30  string(string_part_ref source) : m_content(rcnew_t<string8>(source)) {}
31  template<typename TSource> string(const TSource & p_source);
32 
33  string(const string& other) : m_content(other.m_content) {}
34  string(string&& other) : m_content(std::move(other.m_content)) {}
35 
36  const string& operator=(const string& other) {m_content = other.m_content; return *this;}
37  const string& operator=(string&& other) {m_content = std::move(other.m_content); return *this;}
38 
39 
40  string const & toString() const {return *this;}
41 
42  //warning, not length-checked anymore!
43  static string g_concatenateRaw(const char * item1, t_size len1, const char * item2, t_size len2) {
44  t_dataImpl impl; impl.new_t();
45  char * buffer = impl->lock_buffer(len1+len2);
46  memcpy_t(buffer,item1,len1);
47  memcpy_t(buffer+len1,item2,len2);
48  impl->unlock_buffer();
49  return string(t_data(impl));
50  }
51 
52  string operator+(const string& p_item2) const {
53  return g_concatenateRaw(ptr(),length(),p_item2.ptr(),p_item2.length());
54  }
55  string operator+(const char * p_item2) const {
56  return g_concatenateRaw(ptr(),length(),p_item2,strlen(p_item2));
57  }
58 
59  template<typename TSource> string operator+(const TSource & p_item2) const;
60 
61  template<typename TSource>
62  const string & operator+=(const TSource & p_item) {
63  *this = *this + p_item;
64  return *this;
65  }
66 
67  string subString(t_size base) const {
68  if (base > length()) throw exception_overflow();
69  return string(ptr() + base);
70  }
71  string subString(t_size base, t_size count) const {
72  return string(ptr() + base,count);
73  }
74 
75  string toLower() const {
76  string8_fastalloc temp; temp.prealloc(128);
77  stringToLowerAppend(temp,ptr(),~0);
78  return string(temp.get_ptr());
79  }
80  string toUpper() const {
81  string8_fastalloc temp; temp.prealloc(128);
82  stringToUpperAppend(temp,ptr(),~0);
83  return string(temp.get_ptr());
84  }
85 
86  string clone() const {return string(ptr());}
87 
89  t_size indexOf(char c,t_size base = 0) const;
91  t_size lastIndexOf(char c,t_size base = ~0) const;
93  t_size indexOf(stringp s,t_size base = 0) const;
95  t_size lastIndexOf(stringp s,t_size base = ~0) const;
97  t_size indexOfAnyChar(stringp s,t_size base = 0) const;
99  t_size lastIndexOfAnyChar(stringp s,t_size base = ~0) const;
100 
101  bool contains(char c) const;
102  bool contains(stringp s) const;
103 
104  bool containsAnyChar(stringp s) const;
105 
106  bool startsWith(char c) const;
107  bool startsWith(string s) const;
108  bool endsWith(char c) const;
109  bool endsWith(string s) const;
110 
111  char firstChar() const;
112  char lastChar() const;
113 
114  string replace(stringp strOld, stringp strNew) const;
115 
116  static int g_compare(const string & p_item1, const string & p_item2) {return strcmp(p_item1.ptr(),p_item2.ptr());}
117  bool operator==(const string& p_other) const {return g_compare(*this,p_other) == 0;}
118  bool operator!=(const string& p_other) const {return g_compare(*this,p_other) != 0;}
119  bool operator<(const string& p_other) const {return g_compare(*this,p_other) < 0;}
120  bool operator>(const string& p_other) const {return g_compare(*this,p_other) > 0;}
121  bool operator<=(const string& p_other) const {return g_compare(*this,p_other) <= 0;}
122  bool operator>=(const string& p_other) const {return g_compare(*this,p_other) >= 0;}
123 
124  const char * ptr() const {return m_content->get_ptr();}
125  const char * get_ptr() const {return m_content->get_ptr();}
126  const char * c_str() const { return get_ptr(); }
127  t_size length() const {return m_content->get_length();}
128  t_size get_length() const {return m_content->get_length();}
129 
130  void set_string(const char * ptr, t_size len = ~0) {
131  *this = string(ptr, len);
132  }
133 
134  static bool isNonTextChar(char c) {return c >= 0 && c < 32;}
135 
136  char operator[](t_size p_index) const {
137  PFC_ASSERT(p_index <= length());
138  return ptr()[p_index];
139  }
140  bool isEmpty() const {return length() == 0;}
141 
143  protected:
144  template<typename T> static const char * myStringToPtr(const T& val) {return stringToPtr(val);}
145  static const char * myStringToPtr(string_part_ref) {
146  PFC_ASSERT(!"Should never get here"); throw exception_invalid_params();
147  }
148  };
149 
151  public:
152  template<typename T1,typename T2>
153  static int compare(T1 const& v1, T2 const& v2) {
155  return compare_ex(stringToRef(v1), stringToRef(v2));
156  } else {
157  return strcmp(myStringToPtr(v1),myStringToPtr(v2));
158  }
159  }
161  return strcmp_ex(v1.m_ptr, v1.m_len, v2.m_ptr, v2.m_len);
162  }
163  static int compare_ex(const char * v1, t_size l1, const char * v2, t_size l2) {
164  return strcmp_ex(v1, l1, v2, l2);
165  }
166  };
168  public:
169  template<typename T1,typename T2>
170  static int compare(T1 const& v1, T2 const& v2) {
173  } else {
174  return stringCompareCaseInsensitive(myStringToPtr(v1),myStringToPtr(v2));
175  }
176  }
177  };
179  public:
180  template<typename T1,typename T2>
181  static int compare(T1 const& v1, T2 const& v2) {
183  return compare_ex(stringToRef(v1), stringToRef(v2));
184  } else {
185  return stricmp_ascii(myStringToPtr(v1),myStringToPtr(v2));
186  }
187  }
189  return stricmp_ascii_ex(v1.m_ptr, v1.m_len, v2.m_ptr, v2.m_len);
190  }
191  static int compare_ex(const char * v1, t_size l1, const char * v2, t_size l2) {
192  return stricmp_ascii_ex(v1, l1, v2, l2);
193  }
194  };
195 
196  static bool g_equals(const string & p_item1, const string & p_item2) {return p_item1 == p_item2;}
197  static bool g_equalsCaseInsensitive(const string & p_item1, const string & p_item2) {return comparatorCaseInsensitive::compare(p_item1,p_item2) == 0;}
198 
199  t_data _content() const {return m_content;}
200  private:
201  t_data m_content;
202  };
203 
204  template<typename T> inline string toString(T const& val) {return val.toString();}
205  template<> inline string toString(t_int64 const& val) {return format_int(val).get_ptr();}
206  template<> inline string toString(t_int32 const& val) {return format_int(val).get_ptr();}
207  template<> inline string toString(t_int16 const& val) {return format_int(val).get_ptr();}
208  template<> inline string toString(t_uint64 const& val) {return format_uint(val).get_ptr();}
209  template<> inline string toString(t_uint32 const& val) {return format_uint(val).get_ptr();}
210  template<> inline string toString(t_uint16 const& val) {return format_uint(val).get_ptr();}
211  template<> inline string toString(float const& val) {return format_float(val).get_ptr();}
212  template<> inline string toString(double const& val) {return format_float(val).get_ptr();}
213  template<> inline string toString(char const& val) {return string(&val,1);}
214  inline const char * toString(std::exception const& val) {return val.what();}
215 
216  template<typename TSource> string::string(const TSource & p_source) {
217  *this = pfc::toString(p_source);
218  }
219  template<typename TSource> string string::operator+(const TSource & p_item2) const {
220  return *this + pfc::toString(p_item2);
221  }
222 
224  class stringp {
225  public:
226  stringp(const char * ptr) : m_ptr(ptr) {}
227  stringp(string const &s) : m_ptr(s.ptr()), m_s(s._content()) {}
228  stringp(string_base const &s) : m_ptr(s.get_ptr()) {}
229  template<typename TWhat> stringp(const TWhat& in) : m_ptr(in.toString()) {}
230 
231  operator const char*() const {return m_ptr;}
232  const char * ptr() const {return m_ptr;}
233  const char * get_ptr() const {return m_ptr;}
234  string str() const {return m_s.is_valid() ? string(m_s) : string(m_ptr);}
235  operator string() const {return str();}
236  string toString() const {return str();}
237  t_size length() const {return m_s.is_valid() ? m_s->length() : strlen(m_ptr);}
238  const char * c_str() const { return ptr(); }
239  private:
240  const char * const m_ptr;
242  };
243 
244  template<typename TList>
245  string stringCombineList(const TList & list, stringp separator) {
246  typename TList::const_iterator iter = list.first();
247  string acc;
248  if (iter.is_valid()) {
249  acc = *iter;
250  for(++iter; iter.is_valid(); ++iter) {
251  acc = acc + separator + *iter;
252  }
253  }
254  return acc;
255  }
256 
257  class string;
258  template<> class traits_t<string> : public traits_default {};
259 
260 }
static int compare_ex(string_part_ref v1, string_part_ref v2)
Definition: stringNew.h:160
string operator+(const string &p_item2) const
Definition: stringNew.h:52
t_size length() const
Definition: stringNew.h:127
string(t_data const &p_source)
Definition: stringNew.h:29
const string & operator+=(const TSource &p_item)
Definition: stringNew.h:62
static const char * myStringToPtr(const T &val)
Definition: stringNew.h:144
void new_t()
Definition: rcptr.h:101
const char * stringToPtr(T const &val)
Definition: string_base.h:1020
rcptr_t< string8 > t_dataImpl
Definition: stringNew.h:22
string operator+(const char *p_item2) const
Definition: stringNew.h:55
const char *const m_ptr
Definition: stringNew.h:240
void set_string(const char *p_string, t_size p_length=~0)
Definition: stringNew.h:7
string(const string &other)
Definition: stringNew.h:33
uint64_t t_uint64
Definition: int_types.h:3
static int compare(T1 const &v1, T2 const &v2)
Definition: stringNew.h:153
t_data _content() const
Definition: stringNew.h:199
string(char *p_source, t_size p_sourceLen)
Definition: stringNew.h:28
stringp(const TWhat &in)
Definition: stringNew.h:229
stringp(string const &s)
Definition: stringNew.h:227
string(const char *p_source)
Definition: stringNew.h:25
int compare(t1 const &p1, t2 const &p2)
Definition: pathUtils.h:29
static int compare(T1 const &v1, T2 const &v2)
Definition: stringNew.h:181
string const & toString() const
Definition: stringNew.h:40
const char * get_ptr() const
Definition: string_base.h:580
STL namespace.
string(char *p_source)
Definition: stringNew.h:27
const char * m_ptr
Definition: string_base.h:13
bool operator==(const string &p_other) const
Definition: stringNew.h:117
const char * c_str() const
Definition: stringNew.h:126
rcptr_t< string_base const > t_data
Definition: stringNew.h:21
int16_t t_int16
Definition: int_types.h:6
void add_string(const char *p_string, t_size p_length=~0)
Definition: stringNew.h:6
int stricmp_ascii_ex(const char *const s1, t_size const len1, const char *const s2, t_size const len2)
const char * get_ptr() const
Definition: string_base.h:568
string subString(t_size base) const
Definition: stringNew.h:67
char operator[](t_size p_index) const
Definition: stringNew.h:136
string stringCombineList(const TList &list, stringp separator)
Definition: stringNew.h:245
string(string &&other)
Definition: stringNew.h:34
int stringCompareCaseInsensitiveEx(string_part_ref s1, string_part_ref s2)
string clone() const
Definition: stringNew.h:86
void unlock_buffer()
Definition: stringNew.h:11
const char * ptr() const
Definition: stringNew.h:124
string::t_data m_s
Definition: stringNew.h:241
int32_t t_int32
Definition: int_types.h:4
t_data m_content
Definition: stringNew.h:201
char * lock_buffer(t_size p_requested_length)
Definition: stringNew.h:10
t_size get_length() const
Definition: stringNew.h:128
static bool isNonTextChar(char c)
Definition: stringNew.h:134
int strcmp_ex(const char *p1, t_size n1, const char *p2, t_size n2)
string(const char *p_source, t_size p_sourceLen)
Definition: stringNew.h:26
bool operator<=(const string &p_other) const
Definition: stringNew.h:121
"String parameter" helper class, to use in function parameters, allowing functions to take any type o...
Definition: stringNew.h:224
void stringToLowerAppend(string_base &out, const char *src, t_size len)
t_size get_length() const
Definition: stringNew.h:9
size_t t_size
Definition: int_types.h:48
static const char * myStringToPtr(string_part_ref)
Definition: stringNew.h:145
const string & operator=(const string &other)
Definition: stringNew.h:36
string toString() const
Definition: stringNew.h:236
const string & operator=(string &&other)
Definition: stringNew.h:37
static int compare_ex(const char *v1, t_size l1, const char *v2, t_size l2)
Definition: stringNew.h:191
bool operator>=(const string &p_other) const
Definition: stringNew.h:122
uint16_t t_uint16
Definition: int_types.h:7
static int compare_ex(string_part_ref v1, string_part_ref v2)
Definition: stringNew.h:188
string str() const
Definition: stringNew.h:234
void stringToUpperAppend(string_base &out, const char *src, t_size len)
rcptr_t< t_object > rcnew_t()
Definition: rcptr.h:215
string subString(t_size base, t_size count) const
Definition: stringNew.h:71
bool operator<(const string &p_other) const
Definition: stringNew.h:119
const char * get_ptr() const
Definition: string_base.h:556
static int compare(T1 const &v1, T2 const &v2)
Definition: stringNew.h:170
const char * get_ptr() const
Definition: stringNew.h:5
const char * ptr() const
Definition: stringNew.h:232
t_size length() const
Definition: stringNew.h:237
std::exception exception
Definition: primitives.h:193
const char * toString() const
Definition: string_base.h:206
string toString(T const &val)
Definition: stringNew.h:204
stringp(const char *ptr)
Definition: stringNew.h:226
string toUpper() const
Definition: stringNew.h:80
const char * get_ptr() const
Definition: stringNew.h:125
int stringCompareCaseInsensitive(const char *s1, const char *s2)
bool isEmpty() const
Definition: stringNew.h:140
static int compare_ex(const char *v1, t_size l1, const char *v2, t_size l2)
Definition: stringNew.h:163
int stricmp_ascii(const char *s1, const char *s2)
const char * get_ptr() const
Definition: stringNew.h:233
New EXPERIMENTAL string class, allowing efficient copies and returning from functions. Does not implement the string_base interface so you still need string8 in many cases. Safe to pass between DLLs, but since a reference is used, objects possibly created by other DLLs must be released before owning DLLs are unloaded.
Definition: stringNew.h:19
static bool g_equalsCaseInsensitive(const string &p_item1, const string &p_item2)
Definition: stringNew.h:197
static bool g_equals(const string &p_item1, const string &p_item2)
Definition: stringNew.h:196
void memcpy_t(t_dst *p_dst, const t_src *p_src, t_size p_count)
Definition: primitives.h:611
static string g_concatenateRaw(const char *item1, t_size len1, const char *item2, t_size len2)
Definition: stringNew.h:43
bool operator>(const string &p_other) const
Definition: stringNew.h:120
string(string_part_ref source)
Definition: stringNew.h:30
string toLower() const
Definition: stringNew.h:75
void truncate(t_size len)
Definition: stringNew.h:8
stringp(string_base const &s)
Definition: stringNew.h:228
static int g_compare(const string &p_item1, const string &p_item2)
Definition: stringNew.h:116
bool operator!=(const string &p_other) const
Definition: stringNew.h:118
void set_string(const char *ptr, t_size len=~0)
Definition: stringNew.h:130
string8_t< pfc::alloc_fast_aggressive > string8_fastalloc
Definition: string_base.h:436
static string_part_ref stringToRef(T const &val)
Definition: string_base.h:1023
uint32_t t_uint32
Definition: int_types.h:5
const char * c_str() const
Definition: stringNew.h:238
int64_t t_int64
Definition: int_types.h:2
t_size length() const
For compatibility with old conventions.
Definition: string_base.h:209