stringUnit.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "console/console.h"
  23. #include "string/stringUnit.h"
  24. #include "string/stringTable.h"
  25. namespace StringUnit
  26. {
  27. static char _returnBuffer[4096];
  28. StringTableEntry getStringTableUnit(const char* string, U32 index, const char* set)
  29. {
  30. return StringTable->insert( getUnit( string, index, set ) );
  31. }
  32. const char* getUnit(const char* string, U32 index, const char* set)
  33. {
  34. U32 sz;
  35. while(index--)
  36. {
  37. if(!*string)
  38. return "";
  39. sz = dStrcspn(string, set);
  40. if (string[sz] == 0)
  41. return "";
  42. string += (sz + 1);
  43. }
  44. sz = dStrcspn(string, set);
  45. if (sz == 0)
  46. return "";
  47. AssertFatal( sz + 1 < sizeof( _returnBuffer ), "Size of returned string too large for return buffer" );
  48. char *ret = &_returnBuffer[0];
  49. dStrncpy(ret, string, sz);
  50. ret[sz] = '\0';
  51. return ret;
  52. }
  53. const char* getUnits(const char* string, S32 startIndex, S32 endIndex, const char* set)
  54. {
  55. // [neo, 5/11/2007 - #2998]
  56. // Range check
  57. if( startIndex > endIndex )
  58. return "";
  59. S32 sz;
  60. S32 index = startIndex;
  61. while(index--)
  62. {
  63. if(!*string)
  64. return "";
  65. sz = dStrcspn(string, set);
  66. if (string[sz] == 0)
  67. return "";
  68. string += (sz + 1);
  69. }
  70. const char *startString = string;
  71. while(startIndex <= endIndex--)
  72. {
  73. sz = dStrcspn(string, set);
  74. string += sz;
  75. if (*string == 0)
  76. break;
  77. string++;
  78. }
  79. if(!*string)
  80. string++;
  81. // [neo, 5/11/2007 - #2998]
  82. // Another case of the U32 wrapping from 0 to boom! If totalSize < 0 it will
  83. // wrap to somewhere around U32 max and that is a lot for dStrncpy to overrun!
  84. //U32 totalSize = (U32(string - startString));
  85. S32 totalSize = (S32)(string - startString);
  86. AssertFatal( totalSize < sizeof( _returnBuffer ), "Size of returned string too large for return buffer" );
  87. if( totalSize > 0 )
  88. {
  89. char *ret = &_returnBuffer[0];
  90. dStrncpy(ret, startString, totalSize - 1);
  91. ret[totalSize-1] = '\0';
  92. return ret;
  93. }
  94. return "";
  95. }
  96. U32 getUnitCount(const char *string, const char *set)
  97. {
  98. U32 count = 0;
  99. U8 last = 0;
  100. while(*string)
  101. {
  102. last = *string++;
  103. for(U32 i =0; set[i]; i++)
  104. {
  105. if(last == set[i])
  106. {
  107. count++;
  108. last = 0;
  109. break;
  110. }
  111. }
  112. }
  113. if(last)
  114. count++;
  115. return count;
  116. }
  117. const char* setUnit(const char *string, U32 index, const char *replace, const char *set)
  118. {
  119. U32 sz;
  120. const char *start = string;
  121. AssertFatal( dStrlen(string) + dStrlen(replace) + 1 < sizeof( _returnBuffer ), "Size of returned string too large for return buffer" );
  122. char *ret = &_returnBuffer[0];
  123. ret[0] = '\0';
  124. U32 padCount = 0;
  125. while(index--)
  126. {
  127. sz = dStrcspn(string, set);
  128. if(string[sz] == 0)
  129. {
  130. string += sz;
  131. padCount = index + 1;
  132. break;
  133. }
  134. else
  135. string += (sz + 1);
  136. }
  137. // copy first chunk
  138. sz = string-start;
  139. dStrncpy(ret, start, sz);
  140. for(U32 i = 0; i < padCount; i++)
  141. ret[sz++] = set[0];
  142. // replace this unit
  143. ret[sz] = '\0';
  144. dStrcat(ret, replace);
  145. // copy remaining chunks
  146. sz = dStrcspn(string, set); // skip chunk we're replacing
  147. if(!sz && !string[sz])
  148. return ret;
  149. string += sz;
  150. dStrcat(ret, string);
  151. return ret;
  152. }
  153. const char* removeUnit(const char *string, U32 index, const char *set)
  154. {
  155. U32 sz;
  156. const char *start = string;
  157. AssertFatal( dStrlen(string) + 1 < sizeof( _returnBuffer ), "Size of returned string too large for return buffer" );
  158. char *ret = &_returnBuffer[0];
  159. ret[0] = '\0';
  160. while(index--)
  161. {
  162. sz = dStrcspn(string, set);
  163. // if there was no unit out there... return the original string
  164. if(string[sz] == 0)
  165. return start;
  166. else
  167. string += (sz + 1);
  168. }
  169. // copy first chunk
  170. sz = string-start;
  171. dStrncpy(ret, start, sz);
  172. ret[sz] = 0;
  173. // copy remaining chunks
  174. sz = dStrcspn(string, set); // skip chunk we're removing
  175. if(string[sz] == 0) { // if that was the last...
  176. if(string != start) {
  177. ret[string - start - 1] = 0; // then kill any trailing delimiter
  178. }
  179. return ret; // and bail
  180. }
  181. string += sz + 1; // skip the extra field delimiter
  182. dStrcat(ret, string);
  183. return ret;
  184. }
  185. };