ustring.h
1 /*************************************************************************/
2 /* ustring.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef RSTRING_H
30 #define RSTRING_H
31 
32 #include "typedefs.h"
33 #include "vector.h"
34 #include "array.h"
35 
42 class CharString : public Vector<char> {
43 public:
44  int length() const { return size() ? size()-1 : 0; }
45  const char *get_data() const;
46  operator const char*() {return get_data();};
47 };
48 
49 #ifndef CHARTYPE_16BITS
50 typedef wchar_t CharType;
51 #else
52 typedef wchar_t uint16_t;
53 #endif
54 
55 struct StrRange {
56 
57  const CharType *c_str;
58  int len;
59 
60  StrRange(const CharType *p_c_str=NULL,int p_len=0) { c_str=p_c_str; len=p_len; }
61 
62 };
63 
64 class String : public Vector<CharType> {
65 
66  void copy_from(const char *p_cstr);
67  void copy_from(const CharType* p_cstr, int p_clip_to=-1);
68  void copy_from(const CharType& p_char);
69 
70 
71 public:
72 
73  enum {
74 
75  npos=-1
76  };
77 
78 
79  bool operator==(const String& p_str) const;
80  bool operator!=(const String& p_str) const;
81  String operator+(const String & p_str) const;
82  //String operator+(CharType p_char) const;
83 
84  String& operator+=(const String &);
85  String& operator+=(CharType p_str);
86  String& operator+=(const char * p_str);
87  String& operator+=(const CharType * p_str);
88 
89  /* Compatibility Operators */
90 
91 
92  void operator=(const char *p_str);
93  void operator=(const CharType *p_str);
94  bool operator==(const char *p_str) const;
95  bool operator==(const CharType *p_str) const;
96  bool operator==(const StrRange &p_str_range) const;
97  bool operator!=(const char *p_str) const;
98  bool operator!=(const CharType *p_str) const;
99  bool operator<(const CharType *p_str) const;
100  bool operator<(const char *p_str) const;
101  bool operator<(String p_str) const;
102  bool operator<=(String p_str) const;
103 
104  signed char casecmp_to(const String& p_str) const;
105  signed char nocasecmp_to(const String& p_str) const;
106 
107  const CharType * c_str() const;
108  /* standard size stuff */
109 
110  int length() const;
111 
112  /* complex helpers */
113  String substr(int p_from,int p_chars) const;
114  int find(String p_str,int p_from=0) const;
115  int find_last(String p_str) const;
116  int findn(String p_str,int p_from=0) const;
117  int rfind(String p_str,int p_from=-1) const;
118  int rfindn(String p_str,int p_from=-1) const;
119  int findmk(const Vector<String>& p_keys,int p_from=0,int *r_key=NULL) const;
120  bool match(const String& p_wildcard) const;
121  bool matchn(const String& p_wildcard) const;
122  bool begins_with(const String& p_string) const;
123  bool begins_with(const char* p_string) const;
124  bool ends_with(const String& p_string) const;
125  String replace_first(String p_key,String p_with) const;
126  String replace(String p_key,String p_with) const;
127  String replacen(String p_key,String p_with) const;
128  String insert(int p_at_pos,String p_string) const;
129  String pad_decimals(int p_digits) const;
130  String pad_zeros(int p_digits) const;
131  String lpad(int min_length,const String& character=" ") const;
132  String rpad(int min_length,const String& character=" ") const;
133  String sprintf(const Array& values, bool* error) const;
134  static String num(double p_num,int p_decimals=-1);
135  static String num_scientific(double p_num);
136  static String num_real(double p_num);
137  static String num_int64(int64_t p_num,int base=10,bool capitalize_hex=false);
138  static String chr(CharType p_char);
139  static String md5(const uint8_t *p_md5);
140  bool is_numeric() const;
141  double to_double() const;
142  float to_float() const;
143  int hex_to_int() const;
144  int to_int() const;
145 
146  int64_t to_int64() const;
147  static int to_int(const char* p_str, int p_len=-1);
148  static double to_double(const char* p_str);
149  static double to_double(const CharType* p_str, const CharType **r_end=NULL);
150  static int64_t to_int(const CharType* p_str,int p_len=-1);
151  String capitalize() const;
152  String camelcase_to_underscore(bool lowercase=true) const;
153 
154  int get_slice_count(String p_splitter) const;
155  String get_slice(String p_splitter,int p_slice) const;
156  String get_slicec(CharType splitter,int p_slice) const;
157 
158  Vector<String> split(const String &p_splitter,bool p_allow_empty=true) const;
159  Vector<String> split_spaces() const;
160  Vector<float> split_floats(const String &p_splitter,bool p_allow_empty=true) const;
161  Vector<float> split_floats_mk(const Vector<String> &p_splitters,bool p_allow_empty=true) const;
162  Vector<int> split_ints(const String &p_splitter,bool p_allow_empty=true) const;
163  Vector<int> split_ints_mk(const Vector<String> &p_splitters,bool p_allow_empty=true) const;
164 
165  static CharType char_uppercase(CharType p_char);
166  static CharType char_lowercase(CharType p_char);
167  String to_upper() const;
168  String to_lower() const;
169 
170  String left(int p_pos) const;
171  String right(int p_pos) const;
172  String strip_edges(bool left = true, bool right = true) const;
173  String strip_escapes() const;
174  String extension() const;
175  String basename() const;
176  String plus_file(const String& p_file) const;
177  CharType ord_at(int p_idx) const;
178 
179  void erase(int p_pos, int p_chars);
180 
181  CharString ascii(bool p_allow_extended=false) const;
182  CharString utf8() const;
183  bool parse_utf8(const char* p_utf8,int p_len=-1); //return true on error
184  static String utf8(const char* p_utf8,int p_len=-1);
185 
186  static uint32_t hash(const CharType* p_str,int p_len); /* hash the string */
187  static uint32_t hash(const CharType* p_str); /* hash the string */
188  static uint32_t hash(const char* p_cstr,int p_len); /* hash the string */
189  static uint32_t hash(const char* p_cstr); /* hash the string */
190  uint32_t hash() const; /* hash the string */
191  uint64_t hash64() const; /* hash the string */
192  String md5_text() const;
193  Vector<uint8_t> md5_buffer() const;
194 
195  inline bool empty() const { return length() == 0; }
196 
197  // path functions
198  bool is_abs_path() const;
199  bool is_rel_path() const;
200  bool is_resource_file() const;
201  String path_to(const String& p_path) const;
202  String path_to_file(const String& p_path) const;
203  String get_base_dir() const;
204  String get_file() const;
205  static String humanize_size(size_t p_size);
206  String simplify_path() const;
207 
208  String xml_escape(bool p_escape_quotes=false) const;
209  String xml_unescape() const;
210  String http_escape() const;
211  String http_unescape() const;
212  String c_escape() const;
213  String c_unescape() const;
214  String json_escape() const;
215  String world_wrap(int p_chars_per_line) const;
216 
217  String percent_encode() const;
218  String percent_decode() const;
219 
220  bool is_valid_identifier() const;
221  bool is_valid_integer() const;
222  bool is_valid_float() const;
223  bool is_valid_html_color() const;
224  bool is_valid_ip_address() const;
225 
229 /* String(CharType p_char);*/
230  inline String() {}
231  String(const char *p_str);
232  String(const CharType *p_str,int p_clip_to_len=-1);
233  String(const StrRange& p_range);
234 
235 
236 };
237 
238 
239 bool operator==(const char*p_chr, const String& p_str);
240 
241 String operator+(const char*p_chr, const String& p_str);
242 String operator+(CharType p_chr, const String& p_str);
243 
244 String itos(int64_t p_val);
245 String rtos(double p_val);
246 String rtoss(double p_val); //scientific version
247 
248 
250 
251  bool operator()(const String& p_a, const String& p_b) const {
252 
253  return p_a.nocasecmp_to(p_b)<0;
254  }
255 };
256 
257  /* end of namespace */
258 
259 
260 #endif
Definition: array.h:38
String()
Definition: ustring.h:230
Definition: ustring.h:42
const char * get_data() const
Definition: ustring.cpp:54
Definition: ustring.h:64
Definition: ustring.h:249
Definition: vector.h:43
Definition: ustring.h:55