PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/source/stringtokenizer.cpp

https://bitbucket.org/val_haris/asc-ai
C++ | 126 lines | 81 code | 28 blank | 17 comment | 28 complexity | 6188717ba0191bcf46ecf9c9d1c9b356 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. /***************************************************************************
  2. stringtokenizer.cpp - description
  3. -------------------
  4. begin : Sun Jan 28 2001
  5. copyright : (C) 2001 by Martin Bickel
  6. email : bickel@asc-hq.org
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #include <cctype>
  17. #include <stdio.h>
  18. #include "stringtokenizer.h"
  19. #include "errors.h"
  20. StringTokenizer :: StringTokenizer ( const ASCString& _str, bool includeOperators_ )
  21. : str( _str ), i ( 0 )
  22. {
  23. includeOperators = includeOperators_ ;
  24. // if ( includeOperators_ ) {
  25. delimitter = "=*/+->";
  26. // }
  27. }
  28. StringTokenizer :: StringTokenizer ( const ASCString& _str, const ASCString& delimitter_ )
  29. : str( _str ), i ( 0 ), includeOperators ( false ), delimitter(delimitter_)
  30. {
  31. }
  32. StringTokenizer :: StringTokenizer ( const ASCString& _str, const char* delimitter_ )
  33. : str( _str ), i ( 0 ), includeOperators ( false ), delimitter(delimitter_)
  34. {
  35. }
  36. int StringTokenizer::CharSpace ( char c )
  37. {
  38. if ( c <= ' ' )
  39. return 0;
  40. const char* d = delimitter.c_str();
  41. do {
  42. if( *d == c && !includeOperators )
  43. return 2;
  44. if ( *d == 0 )
  45. return 1;
  46. d++;
  47. } while ( true );
  48. }
  49. void StringTokenizer::skipTill(char endchar )
  50. {
  51. const char* s = str.c_str();
  52. while ( i < str.length() && s[i] != endchar )
  53. ++i;
  54. }
  55. ASCString StringTokenizer::getNextToken( )
  56. {
  57. while ( i < str.length() && !CharSpace(str[i]) )
  58. i++;
  59. if ( i == str.length() )
  60. return "";
  61. int begin = i;
  62. int cs = CharSpace( str[i] );
  63. do {
  64. i++;
  65. } while ( i < str.length() && CharSpace( str[i] ) == cs );
  66. return str.substr(begin, i-begin);
  67. }
  68. ASCString StringTokenizer::getRemaining( )
  69. {
  70. return str.substr(i);
  71. }
  72. StringSplit :: StringSplit ( const ASCString& _str, const ASCString& delimitter_ )
  73. : str( _str ), i ( 0 ), delimitter(delimitter_)
  74. {
  75. }
  76. bool StringSplit::isDelimitter ( char c )
  77. {
  78. const char* d = delimitter.c_str();
  79. do {
  80. if( *d == c )
  81. return true;
  82. if ( *d == 0 )
  83. return false;
  84. d++;
  85. } while ( true );
  86. }
  87. ASCString StringSplit::getNextToken( )
  88. {
  89. const char* sp = str.c_str();
  90. while ( i < str.length() && isDelimitter(sp[i]) )
  91. i++;
  92. if ( i == str.length() )
  93. return "";
  94. int begin = i;
  95. while ( i < str.length() && !isDelimitter(sp[i]) )
  96. i++;
  97. return str.substr(begin, i-begin);
  98. }