stringUnit.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "core/strings/stringFunctions.h"
  23. #include "core/strings/stringUnit.h"
  24. #include "console/console.h"
  25. namespace StringUnit
  26. {
  27. static char _returnBuffer[ 2048 ];
  28. const char *getUnit(const char *string, U32 index, const char *set, char* buffer, U32 bufferSize)
  29. {
  30. if( !buffer )
  31. {
  32. buffer = _returnBuffer;
  33. bufferSize = sizeof( _returnBuffer );
  34. }
  35. AssertFatal( bufferSize, "StringUnit::getUnit - bufferSize cannot be zero!" );
  36. if( !bufferSize )
  37. return "";
  38. buffer[0] = 0;
  39. dsize_t sz;
  40. while(index--)
  41. {
  42. if(!*string)
  43. return buffer;
  44. sz = dStrcspn(string, set);
  45. if (string[sz] == 0)
  46. return buffer;
  47. string += (sz + 1);
  48. }
  49. sz = dStrcspn(string, set);
  50. if (sz == 0)
  51. return buffer;
  52. AssertWarn( sz + 1 < bufferSize, "Size of returned string too large for return buffer" );
  53. sz = getMin( sz, bufferSize - 1 );
  54. dStrncpy(buffer, string, sz);
  55. buffer[sz] = '\0';
  56. return buffer;
  57. }
  58. const char *getUnits(const char *string, S32 startIndex, S32 endIndex, const char *set)
  59. {
  60. if( startIndex > endIndex )
  61. return "";
  62. dsize_t sz;
  63. S32 index = startIndex;
  64. while(index--)
  65. {
  66. if(!*string)
  67. return "";
  68. sz = dStrcspn(string, set);
  69. if (string[sz] == 0)
  70. return "";
  71. string += (sz + 1);
  72. }
  73. const char *startString = string;
  74. for( U32 i = startIndex; i <= endIndex && *string; i ++ )
  75. {
  76. sz = dStrcspn(string, set);
  77. string += sz;
  78. if( i < endIndex && *string )
  79. string ++;
  80. }
  81. S32 totalSize = ( S32 )( string - startString );
  82. AssertWarn( totalSize + 1 < sizeof( _returnBuffer ), "Size of returned string too large for return buffer" );
  83. totalSize = getMin( totalSize, S32( sizeof( _returnBuffer ) - 1 ) );
  84. if( totalSize > 0 )
  85. {
  86. char *ret = &_returnBuffer[0];
  87. dStrncpy( ret, startString, totalSize );
  88. ret[ totalSize ] = '\0';
  89. return ret;
  90. }
  91. return "";
  92. }
  93. U32 getUnitCount(const char *string, const char *set)
  94. {
  95. U32 count = 0;
  96. U8 last = 0;
  97. while(*string)
  98. {
  99. last = *string++;
  100. for(U32 i =0; set[i]; i++)
  101. {
  102. if(last == set[i])
  103. {
  104. count++;
  105. last = 0;
  106. break;
  107. }
  108. }
  109. }
  110. if(last)
  111. count++;
  112. return count;
  113. }
  114. const char* setUnit(const char *string, U32 index, const char *replace, const char *set)
  115. {
  116. U32 sz;
  117. const char *start = string;
  118. AssertFatal( dStrlen(string) + dStrlen(replace) + 1 < sizeof( _returnBuffer ), "Size of returned string too large for return buffer" );
  119. char *ret = &_returnBuffer[0];
  120. ret[0] = '\0';
  121. U32 padCount = 0;
  122. while(index--)
  123. {
  124. sz = dStrcspn(string, set);
  125. if(string[sz] == 0)
  126. {
  127. string += sz;
  128. padCount = index + 1;
  129. break;
  130. }
  131. else
  132. string += (sz + 1);
  133. }
  134. // copy first chunk
  135. sz = string-start;
  136. dStrncpy(ret, start, sz);
  137. for(U32 i = 0; i < padCount; i++)
  138. ret[sz++] = set[0];
  139. // replace this unit
  140. ret[sz] = '\0';
  141. dStrcat(ret, replace, 2048);
  142. // copy remaining chunks
  143. sz = dStrcspn(string, set); // skip chunk we're replacing
  144. if(!sz && !string[sz])
  145. return ret;
  146. string += sz;
  147. dStrcat(ret, string, 2048);
  148. return ret;
  149. }
  150. const char* removeUnit(const char *string, U32 index, const char *set)
  151. {
  152. U32 sz;
  153. const char *start = string;
  154. AssertFatal( dStrlen(string) + 1 < sizeof( _returnBuffer ), "Size of returned string too large for return buffer" );
  155. char *ret = &_returnBuffer[0];
  156. ret[0] = '\0';
  157. while(index--)
  158. {
  159. sz = dStrcspn(string, set);
  160. // if there was no unit out there... return the original string
  161. if(string[sz] == 0)
  162. return start;
  163. else
  164. string += (sz + 1);
  165. }
  166. // copy first chunk
  167. sz = string-start;
  168. dStrncpy(ret, start, sz);
  169. ret[sz] = 0;
  170. // copy remaining chunks
  171. sz = dStrcspn(string, set); // skip chunk we're removing
  172. if(string[sz] == 0) { // if that was the last...
  173. if(string != start) {
  174. ret[string - start - 1] = 0; // then kill any trailing delimiter
  175. }
  176. return ret; // and bail
  177. }
  178. string += sz + 1; // skip the extra field delimiter
  179. dStrcat(ret, string, 2048);
  180. return ret;
  181. }
  182. }