StringTokenizer.h 3.3 KB

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