/opencollada/Externals/MathMLSolver/src/MathMLString.cpp

https://github.com/AsherBond/MondocosmOS · C++ · 344 lines · 256 code · 54 blank · 34 comment · 18 complexity · 22bc56f7e27f4e6bf3a1798a23eaca86 MD5 · raw file

  1. #include "MathMLSolverStableHeaders.h"
  2. #include "MathMLString.h"
  3. #ifdef _WIN32
  4. # include <windows.h>
  5. #endif
  6. namespace MathML
  7. {
  8. //-----------------------------------------------------------------------
  9. String::size_type StringUtil::lastIndexOf( const String& str, const String& pattern )
  10. {
  11. String::size_type pos = String::npos;
  12. for ( String::size_type i = str.find( pattern );
  13. i != std::string::npos;
  14. i = str.find( pattern, i + 1 )
  15. )
  16. {
  17. pos = i;
  18. }
  19. return pos;
  20. }
  21. //-----------------------------------------------------------------------
  22. String::size_type StringUtil::lastIndexOf( const String& str, const char character )
  23. {
  24. String::size_type pos = String::npos;
  25. for ( String::size_type i = str.find( character );
  26. i != std::string::npos;
  27. i = str.find( character, i + 1 )
  28. )
  29. {
  30. pos = i;
  31. }
  32. return pos;
  33. }
  34. //-----------------------------------------------------------------------
  35. bool StringUtil::startsWith( const String& str, const String& pattern )
  36. {
  37. String::size_type pos = str.find( pattern );
  38. return pos == 0 ? true : false;
  39. }
  40. //-----------------------------------------------------------------------
  41. bool StringUtil::startsWith( const String& str, char character )
  42. {
  43. String::size_type pos = str.find( character );
  44. return pos == 0 ? true : false;
  45. }
  46. //-----------------------------------------------------------------------
  47. bool StringUtil::endsWith( const String& str, const String& pattern )
  48. {
  49. String::size_type pos = str.find( pattern );
  50. if ( pos == String::npos )
  51. return false;
  52. return pos == ( str.size() - pattern.size() ) ? true : false;
  53. }
  54. //-----------------------------------------------------------------------
  55. bool StringUtil::endsWith( const String& str, char character )
  56. {
  57. String::size_type pos = str.find( character );
  58. return pos == ( str.size() - 1 ) ? true : false;
  59. }
  60. //-----------------------------------------------------------------
  61. String StringUtil::replaceAll( const String& str, char character, const String& replacement )
  62. {
  63. String string = str;
  64. String::size_type position = string.find_last_of( character );
  65. while ( position != String::npos )
  66. {
  67. string.replace( position, 1, replacement );
  68. position = string.find_last_of( character );
  69. }
  70. return string;
  71. }
  72. //-----------------------------------------------------------------
  73. String StringUtil::replaceAll( const String& str, const String& pattern, const String& replacement )
  74. {
  75. String string = str;
  76. size_t position = string.find( pattern );
  77. while ( position != String::npos )
  78. {
  79. string.replace( position, pattern.size(), replacement );
  80. position = string.find( pattern );
  81. }
  82. return string;
  83. }
  84. //-----------------------------------------------------------------
  85. String StringUtil::replaceAll( const String& str, const String& pattern, char replacement )
  86. {
  87. String replaceStr;
  88. replaceStr += replacement;
  89. return replaceAll( str, pattern, replaceStr );
  90. }
  91. //-----------------------------------------------------------------------
  92. MathML::String StringUtil::replaceAll( const String& str, char pattern, char replacement )
  93. {
  94. String string = str;
  95. size_t position = string.find( pattern );
  96. while ( position != String::npos )
  97. {
  98. string[ position ] = replacement;
  99. position = string.find( pattern );
  100. }
  101. return string;
  102. }
  103. //-----------------------------------------------------------------------
  104. MathML::String StringUtil::valueOf( unsigned char value )
  105. {
  106. std::stringstream ss;
  107. ss << value << std::ends;
  108. return ss.str();
  109. }
  110. //-----------------------------------------------------------------------
  111. MathML::String StringUtil::valueOf( unsigned short value )
  112. {
  113. std::stringstream ss;
  114. ss << value << std::ends;
  115. return ss.str();
  116. }
  117. //-----------------------------------------------------------------------
  118. MathML::String StringUtil::valueOf( unsigned int value )
  119. {
  120. std::stringstream ss;
  121. ss << value << std::ends;
  122. return ss.str();
  123. }
  124. //-----------------------------------------------------------------------
  125. MathML::String StringUtil::valueOf( unsigned long value )
  126. {
  127. std::stringstream ss;
  128. ss << value << std::ends;
  129. return ss.str();
  130. }
  131. //-----------------------------------------------------------------------
  132. MathML::String StringUtil::valueOf( unsigned long long value )
  133. {
  134. std::stringstream ss;
  135. ss << value << std::ends;
  136. return ss.str();
  137. }
  138. //-----------------------------------------------------------------------
  139. MathML::String StringUtil::valueOf( char value )
  140. {
  141. std::stringstream ss;
  142. ss << value << std::ends;
  143. return ss.str();
  144. }
  145. //-----------------------------------------------------------------------
  146. MathML::String StringUtil::valueOf( short value )
  147. {
  148. std::stringstream ss;
  149. ss << value << std::ends;
  150. return ss.str();
  151. }
  152. //-----------------------------------------------------------------------
  153. MathML::String StringUtil::valueOf( int value )
  154. {
  155. std::stringstream ss;
  156. ss << value << std::ends;
  157. return ss.str();
  158. }
  159. //-----------------------------------------------------------------------
  160. MathML::String StringUtil::valueOf( long value )
  161. {
  162. std::stringstream ss;
  163. ss << value << std::ends;
  164. return ss.str();
  165. }
  166. //-----------------------------------------------------------------------
  167. MathML::String StringUtil::valueOf( long long value )
  168. {
  169. std::stringstream ss;
  170. ss << value << std::ends;
  171. return ss.str();
  172. }
  173. //-----------------------------------------------------------------------
  174. MathML::String StringUtil::valueOf( float value )
  175. {
  176. std::stringstream ss;
  177. ss << value << std::ends;
  178. return ss.str();
  179. }
  180. //-----------------------------------------------------------------------
  181. MathML::String StringUtil::valueOf( double value )
  182. {
  183. std::stringstream ss;
  184. ss << value << std::ends;
  185. return ss.str();
  186. }
  187. //-----------------------------------------------------------------------
  188. int StringUtil::caseCompare( const String& str1, const String& str2 )
  189. {
  190. String tmp1( str1 );
  191. toLowerCase( tmp1 );
  192. String tmp2( str2 );
  193. toLowerCase( tmp2 );
  194. return tmp1.compare( str2 );
  195. }
  196. //-----------------------------------------------------------------------
  197. void StringUtil::toLowerCase( String& str )
  198. {
  199. std::transform(
  200. str.begin(),
  201. str.end(),
  202. str.begin(),
  203. tolower );
  204. }
  205. //-----------------------------------------------------------------------
  206. void StringUtil::toUpperCase( String& str )
  207. {
  208. std::transform(
  209. str.begin(),
  210. str.end(),
  211. str.begin(),
  212. toupper );
  213. }
  214. //-----------------------------------------------------------------------
  215. unsigned short StringUtil::parseUnsignedShort( const String& str )
  216. {
  217. unsigned short value;
  218. std::istringstream inStream( str );
  219. inStream >> value;
  220. return value;
  221. }
  222. //-----------------------------------------------------------------------
  223. unsigned int StringUtil::parseUnsignedInt( const String& str )
  224. {
  225. unsigned int value;
  226. std::istringstream inStream( str );
  227. inStream >> value;
  228. return value;
  229. }
  230. //-----------------------------------------------------------------------
  231. unsigned long StringUtil::parseUnsignedLong( const String& str )
  232. {
  233. unsigned long value;
  234. std::istringstream inStream( str );
  235. inStream >> value;
  236. return value;
  237. }
  238. //-----------------------------------------------------------------------
  239. short StringUtil::parseShort( const String& str )
  240. {
  241. short value;
  242. std::istringstream inStream( str );
  243. inStream >> value;
  244. return value;
  245. }
  246. //-----------------------------------------------------------------------
  247. int StringUtil::parseInt( const String& str )
  248. {
  249. int value;
  250. std::istringstream inStream( str );
  251. inStream >> value;
  252. return value;
  253. }
  254. //-----------------------------------------------------------------------
  255. long StringUtil::parseLong( const String& str )
  256. {
  257. long value;
  258. std::istringstream inStream( str );
  259. inStream >> value;
  260. return value;
  261. }
  262. //-----------------------------------------------------------------------
  263. double StringUtil::parseDouble( const String& str )
  264. {
  265. double value;
  266. std::istringstream inStream( str );
  267. inStream >> value;
  268. return value;
  269. }
  270. //-----------------------------------------------------------------
  271. String StringUtil::removeWhitespaces( const String &string )
  272. {
  273. String whitespaces ( " \t\f\v\n\r" );
  274. String result = string;
  275. String::size_type found = string.find_first_of( whitespaces );
  276. while ( found != String::npos )
  277. {
  278. result.erase( found, 1 );
  279. found = result.find_first_of( whitespaces );
  280. }
  281. return result;
  282. }
  283. //-----------------------------------------------------------------
  284. MathML::String StringUtil::valueOfUnfinalized( int value )
  285. {
  286. std::stringstream ss;
  287. ss << value;
  288. return ss.str();
  289. }
  290. }