ustring.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*************************************************************************/
  2. /* ustring.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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. #include "typedefs.h"
  32. #include "vector.h"
  33. #include "array.h"
  34. /**
  35. @author red <red@killy>
  36. */
  37. class CharString : public Vector<char> {
  38. public:
  39. int length() const { return size() ? size()-1 : 0; }
  40. const char *get_data() const;
  41. operator const char*() {return get_data();};
  42. };
  43. #ifndef CHARTYPE_16BITS
  44. typedef wchar_t CharType;
  45. #else
  46. typedef wchar_t uint16_t;
  47. #endif
  48. struct StrRange {
  49. const CharType *c_str;
  50. int len;
  51. StrRange(const CharType *p_c_str=NULL,int p_len=0) { c_str=p_c_str; len=p_len; }
  52. };
  53. class String : public Vector<CharType> {
  54. void copy_from(const char *p_cstr);
  55. void copy_from(const CharType* p_cstr, int p_clip_to=-1);
  56. void copy_from(const CharType& p_char);
  57. public:
  58. enum {
  59. npos=-1 ///<for "some" compatibility with std::string (npos is a huge value in std::string)
  60. };
  61. bool operator==(const String& p_str) const;
  62. bool operator!=(const String& p_str) const;
  63. String operator+(const String & p_str) const;
  64. //String operator+(CharType p_char) const;
  65. String& operator+=(const String &);
  66. String& operator+=(CharType p_str);
  67. String& operator+=(const char * p_str);
  68. String& operator+=(const CharType * p_str);
  69. /* Compatibility Operators */
  70. void operator=(const char *p_str);
  71. void operator=(const CharType *p_str);
  72. bool operator==(const char *p_str) const;
  73. bool operator==(const CharType *p_str) const;
  74. bool operator==(const StrRange &p_str_range) const;
  75. bool operator!=(const char *p_str) const;
  76. bool operator!=(const CharType *p_str) const;
  77. bool operator<(const CharType *p_str) const;
  78. bool operator<(const char *p_str) const;
  79. bool operator<(String p_str) const;
  80. bool operator<=(String p_str) const;
  81. signed char casecmp_to(const String& p_str) const;
  82. signed char nocasecmp_to(const String& p_str) const;
  83. const CharType * c_str() const;
  84. /* standard size stuff */
  85. int length() const;
  86. /* complex helpers */
  87. String substr(int p_from,int p_chars) const;
  88. int find(String p_str,int p_from=0) const; ///< return <0 if failed
  89. int find_last(String p_str) const; ///< return <0 if failed
  90. int findn(String p_str,int p_from=0) const; ///< return <0 if failed, case insensitive
  91. int rfind(String p_str,int p_from=-1) const; ///< return <0 if failed
  92. int rfindn(String p_str,int p_from=-1) const; ///< return <0 if failed, case insensitive
  93. int findmk(const Vector<String>& p_keys,int p_from=0,int *r_key=NULL) const; ///< return <0 if failed
  94. bool match(const String& p_wildcard) const;
  95. bool matchn(const String& p_wildcard) const;
  96. bool begins_with(const String& p_string) const;
  97. bool begins_with(const char* p_string) const;
  98. bool ends_with(const String& p_string) const;
  99. String replace_first(String p_key,String p_with) const;
  100. String replace(String p_key,String p_with) const;
  101. String replacen(String p_key,String p_with) const;
  102. String insert(int p_at_pos,String p_string) const;
  103. String pad_decimals(int p_digits) const;
  104. String pad_zeros(int p_digits) const;
  105. String lpad(int min_length,const String& character=" ") const;
  106. String rpad(int min_length,const String& character=" ") const;
  107. String sprintf(const Array& values, bool* error) const;
  108. static String num(double p_num,int p_decimals=-1);
  109. static String num_scientific(double p_num);
  110. static String num_real(double p_num);
  111. static String num_int64(int64_t p_num,int base=10,bool capitalize_hex=false);
  112. static String chr(CharType p_char);
  113. static String md5(const uint8_t *p_md5);
  114. bool is_numeric() const;
  115. double to_double() const;
  116. float to_float() const;
  117. int hex_to_int() const;
  118. int to_int() const;
  119. int64_t to_int64() const;
  120. static int to_int(const char* p_str);
  121. static double to_double(const char* p_str);
  122. static double to_double(const CharType* p_str, const CharType **r_end=NULL);
  123. static int64_t to_int(const CharType* p_str,int p_len=-1);
  124. String capitalize() const;
  125. String camelcase_to_underscore() const;
  126. int get_slice_count(String p_splitter) const;
  127. String get_slice(String p_splitter,int p_slice) const;
  128. Vector<String> split(const String &p_splitter,bool p_allow_empty=true) const;
  129. Vector<String> split_spaces() const;
  130. Vector<float> split_floats(const String &p_splitter,bool p_allow_empty=true) const;
  131. Vector<float> split_floats_mk(const Vector<String> &p_splitters,bool p_allow_empty=true) const;
  132. Vector<int> split_ints(const String &p_splitter,bool p_allow_empty=true) const;
  133. Vector<int> split_ints_mk(const Vector<String> &p_splitters,bool p_allow_empty=true) const;
  134. static CharType char_uppercase(CharType p_char);
  135. static CharType char_lowercase(CharType p_char);
  136. String to_upper() const;
  137. String to_lower() const;
  138. String left(int p_pos) const;
  139. String right(int p_pos) const;
  140. String strip_edges() const;
  141. String strip_escapes() const;
  142. String extension() const;
  143. String basename() const;
  144. String plus_file(const String& p_file) const;
  145. CharType ord_at(int p_idx) const;
  146. void erase(int p_pos, int p_chars);
  147. CharString ascii(bool p_allow_extended=false) const;
  148. CharString utf8() const;
  149. bool parse_utf8(const char* p_utf8,int p_len=-1); //return true on error
  150. static String utf8(const char* p_utf8,int p_len=-1);
  151. static uint32_t hash(const CharType* p_str,int p_len); /* hash the string */
  152. static uint32_t hash(const CharType* p_str); /* hash the string */
  153. static uint32_t hash(const char* p_cstr,int p_len); /* hash the string */
  154. static uint32_t hash(const char* p_cstr); /* hash the string */
  155. uint32_t hash() const; /* hash the string */
  156. uint64_t hash64() const; /* hash the string */
  157. String md5_text() const;
  158. Vector<uint8_t> md5_buffer() const;
  159. inline bool empty() const { return length() == 0; }
  160. // path functions
  161. bool is_abs_path() const;
  162. bool is_rel_path() const;
  163. bool is_resource_file() const;
  164. String path_to(const String& p_path) const;
  165. String path_to_file(const String& p_path) const;
  166. String get_base_dir() const;
  167. String get_file() const;
  168. static String humanize_size(size_t p_size);
  169. String simplify_path() const;
  170. String xml_escape(bool p_escape_quotes=false) const;
  171. String xml_unescape() const;
  172. String c_escape() const;
  173. String c_unescape() const;
  174. String percent_encode() const;
  175. String percent_decode() const;
  176. bool is_valid_identifier() const;
  177. bool is_valid_integer() const;
  178. bool is_valid_float() const;
  179. bool is_valid_html_color() const;
  180. bool is_valid_ip_address() const;
  181. /**
  182. * The constructors must not depend on other overloads
  183. */
  184. /* String(CharType p_char);*/
  185. inline String() {}
  186. String(const char *p_str);
  187. String(const CharType *p_str,int p_clip_to_len=-1);
  188. String(const StrRange& p_range);
  189. };
  190. bool operator==(const char*p_chr, const String& p_str);
  191. String operator+(const char*p_chr, const String& p_str);
  192. String operator+(CharType p_chr, const String& p_str);
  193. String itos(int64_t p_val);
  194. String rtos(double p_val);
  195. String rtoss(double p_val); //scientific version
  196. struct NoCaseComparator {
  197. bool operator()(const String& p_a, const String& p_b) const {
  198. return p_a.nocasecmp_to(p_b)<0;
  199. }
  200. };
  201. /* end of namespace */
  202. #endif