bfcstring.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #ifndef _STRING_H_WASABI
  2. #define _STRING_H_WASABI
  3. #ifdef __cplusplus
  4. /**
  5. This is a very basic string class. It is not trying to be a be-all end-all
  6. string class. It's meant to be just enough functionality to avoid STRDUP().
  7. It is available to use in client apps, but no API in Wasabi depends on its
  8. use by client code, so you can ignore it if you like. ;)
  9. Also note that one of the design goals is to use same storage space as
  10. a char *, which means no virtual methods.
  11. @short Basic string class, to replace STRDUP/FREE
  12. @see StringPrintf
  13. @see DebugString
  14. */
  15. #include <bfc/platform/types.h>
  16. //#include <bfc/std.h>
  17. #include <stdarg.h>
  18. class String
  19. {
  20. public:
  21. String(const char *initial_val = NULL);
  22. String(const String &s);
  23. String(const String *s);
  24. ~String();
  25. /**
  26. Returns the value of the string. It is a pointer to the internal storage
  27. used by the class, so you must not assume the pointer will not change,
  28. because it will.
  29. @see getValueSafe();
  30. @ret The value of the string.
  31. */
  32. const char *getValue() const { return val; }
  33. const char *v() const { return getValue(); } // for ease of typing
  34. operator const char *() const { return getValue(); }
  35. /**
  36. Gets the value of the string, or a safe value if the value is NULL.
  37. @param def_val The value to return if the string's value is NULL. Defaults to "".
  38. @see getValue()
  39. @ret The value of the string, or a safe value if the value is NULL.
  40. */
  41. const char *getValueSafe(const char *def_val = "") const; // returns def_val if NULL
  42. /**
  43. Returns the value of the character at a position in the string. If the
  44. position is invalid, returns -1. Note that unless bounds_check is set to
  45. TRUE, the pos will only be checked for negative values.
  46. @param pos The position of the character to return.
  47. @param bounds_check If TRUE, pos is checked against the length of the string.
  48. @see setChar()
  49. @ret The value of the character at the position in the string, or -1 if the position is invalid.
  50. */
  51. int getChar(int pos, int bounds_check = false);
  52. /**
  53. Sets the value of the character at a position in the string. Not multibyte UTF-8 safe yet. No lengthwise bounds checking yet.
  54. @param pos The position to set.
  55. @param value The value to set.
  56. @ret The value of the character set, or -1 if pos < 0.
  57. */
  58. int setChar(int pos, int value);
  59. /**
  60. Sets the value of the string. The given value will be copied.
  61. Can accept NULL. Note that this may cause the
  62. pointer returned by getValue() et al to change.
  63. @param newval The new value of the string, nul-terminated.
  64. @ret The new value of the string.
  65. @see getValue()
  66. */
  67. const char *setValue(const char *newval);
  68. /**
  69. Gets the value of the string in the form of a non-const char *. WARNING: you
  70. don't usually need to call this. If you want to modify the string,
  71. you can generally just use setChar(), or setValue().
  72. @ret The value of the string, casted to a non-const char *.
  73. @see getValue()
  74. @see setValue();
  75. @see setChar();
  76. */
  77. char *getNonConstVal() { return const_cast<char *>(getValue()); }
  78. const char *operator =(const char *newval) { return setValue(newval); }
  79. const char *operator +=(const char *addval)
  80. {
  81. return cat(addval);
  82. }
  83. const char *operator +=(char value);
  84. const char *operator +=(int value);
  85. const char *operator +=(GUID guid);
  86. // copy assignment operator
  87. String &operator =(const String &s)
  88. {
  89. if (this != &s)
  90. setValue(s);
  91. return *this;
  92. }
  93. // comparator operators
  94. inline int operator ==(const char *val) const
  95. {
  96. if (!val) return isempty();
  97. return isequal(val);
  98. }
  99. inline int operator <(const char *val) const
  100. {
  101. return islessthan(val);
  102. }
  103. inline int operator !=(const char *val) const
  104. {
  105. if (!val) return !isempty();
  106. return !isequal(val);
  107. }
  108. inline int operator >(const char *val) const
  109. {
  110. return (!islessthan(val)) && (!isequal(val));
  111. }
  112. inline String operator +(const char *val)
  113. {
  114. String retval = *this;
  115. return retval += val;
  116. }
  117. inline int operator ==(const String &val) const
  118. {
  119. return isequal(val);
  120. }
  121. inline int operator <(const String &val) const
  122. {
  123. return islessthan(val);
  124. }
  125. inline int operator !=(const String &val) const
  126. {
  127. return !isequal(val);
  128. }
  129. inline int operator >(const String &val) const
  130. {
  131. return (!islessthan(val)) && (!isequal(val));
  132. }
  133. inline String operator +(const String &val)
  134. {
  135. String retval = *this;
  136. return retval += val;
  137. }
  138. inline String operator +(const char val)
  139. {
  140. String retval = *this;
  141. return retval += val;
  142. }
  143. /**
  144. Gets the length of the string's value. Note that a 0 length can result from
  145. both a value of NULL and a value of "".
  146. @ret The length of the string's value;
  147. */
  148. int len() const;
  149. /**
  150. Returns TRUE if the string's value is either NULL or "".
  151. @ret TRUE if the string's value is either NULL or "", FALSE otherwise.
  152. */
  153. int isempty() const;
  154. /**
  155. Converts entire string to uppercase. Not multibyte UTF-8 safe yet.
  156. @see tolower()
  157. */
  158. void toupper();
  159. /**
  160. Converts entire string to lowercase. Not multibyte UTF-8 safe yet.
  161. @see tolower()
  162. */
  163. void tolower();
  164. /**
  165. Checks string value equality against a nul-terminated char *.
  166. @param otherval The value to check against. If NULL, will be treated as "".
  167. @ret TRUE if the string matches exactly, FALSE otherwise.
  168. @see iscaseequal()
  169. @see islessthan()
  170. */
  171. int isequal(const char *otherval) const; // basically !strcmp
  172. /**
  173. Checks string value equality against a nul-terminated char *, case insensitively. I.e. "Blah" is case equal to "bLaH".
  174. @param otherval The value to check against. If NULL, will be treated as "".
  175. @ret TRUE if the string matches case-insensitively, FALSE otherwise.
  176. @see isequal()
  177. @see islessthan()
  178. */
  179. int iscaseequal(const char *otherval) const; // basically !strcasecmp
  180. int islessthan(const char *otherval) const; // basically strcmp < 0
  181. /**
  182. Changes all instances of a character to another character throughout the
  183. string. Not multibyte UTF-8 aware yet. Note you can use a 'to' of NULL,
  184. but this practice is not encouraged: try to use trunc() or truncateOnChar() instead.
  185. @param from The character value to modify.
  186. @param to The character value to replace with.
  187. @see trunc()
  188. @see truncateOnChar()
  189. */
  190. void changeChar(int from, int to);
  191. /**
  192. Truncates the string value at the first given character value found. If fromright==TRUE, searches from the right, otherwise goes left-to-right. Not UTF-8 multibyte aware yet.
  193. Ex:
  194. String x("abcd");
  195. x.truncateOnChar('c');
  196. x now contains "ab"
  197. @see changeChar()
  198. */
  199. void truncateOnChar(int which, int fromright = false);
  200. /**
  201. Gets the last character value (rightmost).
  202. @see getChar()
  203. @ret The rightmost character value, or -1 if string is empty.
  204. */
  205. int lastChar(); // -1 if empty
  206. /**
  207. Executes a standard printf type call and sets the string's value to it.
  208. @ret The new value of the string.
  209. @param format The formatting string to use.
  210. */
  211. const char *printf(const char *format, ...);
  212. /**
  213. Concatenates the given value onto the end of the string. NULL will be
  214. treated as "".
  215. @param value The value to concatenate.
  216. @ret The new value of the string.
  217. @see catn()
  218. @see prepend()
  219. */
  220. const char *cat(const char *value);
  221. /**
  222. Concatenates a certain number of characters from the given value onto the end of the string. NULL will be treated as "".
  223. @param value The value to concatenate.
  224. @param len How many characters of value to use.
  225. @ret The new value of the string.
  226. @see cat()
  227. @see prepend()
  228. */
  229. const char *catn(const char *value, int len);
  230. /**
  231. Useful for making directory paths and stuff
  232. adds a string plus a separator character.
  233. i.e.
  234. String x = "/usr/";
  235. x.catPostSeparator("bin", '/');
  236. creates "/usr/bin/"
  237. */
  238. const char *catPostSeparator(const char *value, const char separator);
  239. /**
  240. similiar to above, but puts the separator first
  241. i.e.
  242. String x = "/usr";
  243. x.catPostSeparator('/' "bin");
  244. creates "/usr/bin"
  245. */
  246. const char *catPreSeparator(const char separator, const char *value);
  247. /**
  248. Inserts the given string value at the beginning of the string. NULL will be
  249. treated as "".
  250. @param value The value to insert.
  251. @ret The new value of the string.
  252. @see cat()
  253. @see catn()
  254. */
  255. const char *prepend(const char *value);
  256. // replaces string with n chars of val or length of val, whichever is less.
  257. const char *ncpy(const char *newstr, int numchars);
  258. /**
  259. Copies up to maxlen chars from the string into the destination. Differs from
  260. STRNCPY in that it makes sure the destination is always nul-terminated, so
  261. note that maxlen includes the terminating nul.
  262. @param dest The destination to copy to.
  263. @param maxlen How many bytes, at most, to copy.
  264. */
  265. void strncpyTo(char *dest, int maxlen);
  266. // -----------------------------------------
  267. // Character based find-n-splice methods --
  268. // "l" and "r" prefixes specify to begin at
  269. // front or back of string:
  270. // Returns index of first found, -1 if not found.
  271. int lFindChar(char findval);
  272. int lFindChar(const char *findval); // a list of chars to search for
  273. int rFindChar(char findval);
  274. int rFindChar(const char *findval); // a list of chars to search for
  275. // Splits string at findval. Characters passed by search, including the
  276. // found character, are MOVED to the returned string. If there is no char
  277. // to be found, the entire string is returned and the called instance is
  278. // left empty. (Makes looped splits very easy).
  279. String lSplit(int idxval);
  280. String lSplitChar(char findval);
  281. String lSplitChar(const char *findval);
  282. String rSplit(int idxval);
  283. String rSplitChar(char findval);
  284. String rSplitChar(const char *findval);
  285. // Same as split, except the find char is cut completely.
  286. String lSpliceChar(char findval);
  287. String lSpliceChar(const char *findval);
  288. String rSpliceChar(char findval);
  289. String rSpliceChar(const char *findval);
  290. /**
  291. Replaces all occurences of the value specified by 'find' with the value
  292. specified by 'replace'.
  293. @param find The value to find.
  294. @param replace The value to replace with.
  295. @ret The number of replacements that were executed.
  296. */
  297. int replace(const char *find, const char *replace);
  298. /**
  299. Replaces fields of same character with 0-padded text representation of an int.
  300. Example: blah$$$$.png becomes blah0000.png
  301. */
  302. int replaceNumericField(int value, int fieldchar = '\x24');
  303. // UTF8-Aware "Character Based" Methods
  304. /**
  305. Returns how many characters are in the string value. Same as len(), but multibyte UTF-8 aware.
  306. @see len()
  307. @ret Number of logical UTF-8 character in the string.
  308. */
  309. int numCharacters();
  310. /**
  311. Truncates the length of the string to newlen. If newlen is negative, trims
  312. -newlen characters from the end. Multibyte UTF-8 aware.
  313. @param newlen The new length of the string. If the string is shorter than this, nothing happens. If this value is negative, then the absolute value of newlen is how many characters to trim from the right. I.e. -1 means trim off one character from the end.
  314. @see truncateOnChar()
  315. */
  316. void trunc(int newlen);
  317. void trim(const char *whitespace = " \t\r\n", int left = true, int right = true);
  318. /**
  319. Does a vsprintf. Used the same way as printf(), but with a va_list instead of "...".
  320. @param format The format string to use.
  321. @param args The argument list in va_list format.
  322. @ret The number of characters in the final string.
  323. */
  324. int va_sprintf(const char *format, va_list args);
  325. /**
  326. Ensures that the string drops any memory it might have allocated.
  327. */
  328. void purge();
  329. void swap(String *); // swaps buffers with another string
  330. void swap(String &); // swaps buffers with another string
  331. void own(char *); // take ownership of a buffer
  332. void AppendPath(const char *path);
  333. protected:
  334. char * val;
  335. enum { wastage_allowed = 128 };
  336. };
  337. /**
  338. String class with a printf-style constructor. Otherwise identical to String.
  339. Also takes some standard types, like int, double, and GUID.
  340. @see GUID
  341. @see String
  342. */
  343. class StringPrintf : public String
  344. {
  345. public:
  346. StringPrintf(const char *format = NULL, ...);
  347. StringPrintf(int value);
  348. StringPrintf(double value);
  349. StringPrintf(GUID g);
  350. };
  351. class StringToLower : public String
  352. {
  353. public:
  354. StringToLower(const char *val = NULL) : String(val)
  355. {
  356. tolower();
  357. }
  358. };
  359. class StringToUpper : public String
  360. {
  361. public:
  362. StringToUpper(const char *val = NULL) : String(val)
  363. {
  364. toupper();
  365. }
  366. };
  367. #if defined(NDEBUG) && defined(WASABI_NO_RELEASEMODE_DEBUGSTRINGS)
  368. #define DebugString __noop
  369. #else
  370. #define DebugString _DebugString
  371. #endif
  372. class _DebugString : public String
  373. {
  374. public:
  375. _DebugString(const char *format = NULL, ...);
  376. _DebugString(const String &s);
  377. _DebugString(const String *s);
  378. void debugPrint();
  379. };
  380. #define RecycleString String
  381. // String operators using StringPrintf
  382. inline const char *String::operator +=(char value)
  383. {
  384. return cat(StringPrintf("%c", value)); // Uhm. Shouldn't the string be given
  385. // the "Fast" methods and the Printf be
  386. // built off of that?
  387. }
  388. inline const char *String::operator +=(int value)
  389. {
  390. return cat(StringPrintf("%i", value));
  391. }
  392. inline const char *String::operator +=(GUID guid)
  393. {
  394. return cat(StringPrintf(guid));
  395. }
  396. //
  397. // Global operator overrides to allow string to take over for
  398. // the use of standard operators with const char pointers as
  399. // left hand operands.
  400. inline int operator ==(const char *v1, const String &v2)
  401. {
  402. return v2.isequal(v1);
  403. }
  404. inline int operator !=(const char *v1, const String &v2)
  405. {
  406. return !v2.isequal(v1);
  407. }
  408. inline int operator <(const char *v1, const String &v2)
  409. {
  410. return !v2.islessthan(v1);
  411. }
  412. inline int operator >(const char *v1, const String &v2)
  413. {
  414. return v2.islessthan(v1);
  415. }
  416. /**
  417. Compares two strings. Generally used with PtrListSorted<>
  418. @see PtrListSorted
  419. */
  420. class StringComparator
  421. {
  422. public:
  423. // comparator for sorting
  424. static int compareItem(String *p1, String* p2);
  425. // comparator for searching
  426. static int compareAttrib(const wchar_t *attrib, String *item);
  427. };
  428. #endif // __cplusplus
  429. #endif