StringTokenizer.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // StringTokenizer.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/StringTokenizer.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: StringTokenizer
  9. //
  10. // Definition of the StringTokenizer class.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_StringTokenizer_INCLUDED
  18. #define Foundation_StringTokenizer_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Exception.h"
  21. #include <vector>
  22. #include <cstddef>
  23. namespace Poco {
  24. class Foundation_API StringTokenizer
  25. /// A simple tokenizer that splits a string into
  26. /// tokens, which are separated by separator characters.
  27. /// An iterator is used to iterate over all tokens.
  28. {
  29. public:
  30. enum Options
  31. {
  32. TOK_IGNORE_EMPTY = 1, /// ignore empty tokens
  33. TOK_TRIM = 2 /// remove leading and trailing whitespace from tokens
  34. };
  35. typedef std::vector<std::string> TokenVec;
  36. typedef TokenVec::const_iterator Iterator;
  37. StringTokenizer(const std::string& str, const std::string& separators, int options = 0);
  38. /// Splits the given string into tokens. The tokens are expected to be
  39. /// separated by one of the separator characters given in separators.
  40. /// Additionally, options can be specified:
  41. /// * TOK_IGNORE_EMPTY: empty tokens are ignored
  42. /// * TOK_TRIM: trailing and leading whitespace is removed from tokens.
  43. ~StringTokenizer();
  44. /// Destroys the tokenizer.
  45. Iterator begin() const;
  46. Iterator end() const;
  47. const std::string& operator [] (std::size_t index) const;
  48. /// Returns const reference the index'th token.
  49. /// Throws a RangeException if the index is out of range.
  50. std::string& operator [] (std::size_t index);
  51. /// Returns reference to the index'th token.
  52. /// Throws a RangeException if the index is out of range.
  53. bool has(const std::string& token) const;
  54. /// Returns true if token exists, false otherwise.
  55. std::size_t find(const std::string& token, std::size_t pos = 0) const;
  56. /// Returns the index of the first occurence of the token
  57. /// starting at position pos.
  58. /// Throws a NotFoundException if the token is not found.
  59. std::size_t replace(const std::string& oldToken, const std::string& newToken, std::size_t pos = 0);
  60. /// Starting at position pos, replaces all subsequent tokens having value
  61. /// equal to oldToken with newToken.
  62. /// Returns the number of modified tokens.
  63. std::size_t count() const;
  64. /// Returns the total number of tokens.
  65. std::size_t count(const std::string& token) const;
  66. /// Returns the number of tokens equal to the specified token.
  67. private:
  68. StringTokenizer(const StringTokenizer&);
  69. StringTokenizer& operator = (const StringTokenizer&);
  70. void trim (std::string& token);
  71. TokenVec _tokens;
  72. };
  73. //
  74. // inlines
  75. //
  76. inline StringTokenizer::Iterator StringTokenizer::begin() const
  77. {
  78. return _tokens.begin();
  79. }
  80. inline StringTokenizer::Iterator StringTokenizer::end() const
  81. {
  82. return _tokens.end();
  83. }
  84. inline std::string& StringTokenizer::operator [] (std::size_t index)
  85. {
  86. if (index >= _tokens.size()) throw RangeException();
  87. return _tokens[index];
  88. }
  89. inline const std::string& StringTokenizer::operator [] (std::size_t index) const
  90. {
  91. if (index >= _tokens.size()) throw RangeException();
  92. return _tokens[index];
  93. }
  94. inline std::size_t StringTokenizer::count() const
  95. {
  96. return _tokens.size();
  97. }
  98. } // namespace Poco
  99. #endif // Foundation_StringTokenizer_INCLUDED