strutil.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // $Id$
  15. #ifndef __STRUTIL_H
  16. #define __STRUTIL_H
  17. #include <algorithm>
  18. #include <cctype>
  19. #include <string>
  20. #include <vector>
  21. namespace isc {
  22. namespace strutil {
  23. /// \brief A Set of C++ Utilities for Manipulating Strings
  24. /// \brief Normalize Backslash
  25. ///
  26. /// Only relevant to Windows, this replaces all "\" in a string with "/" and
  27. /// returns the result. On other systems it is a no-op. Note that Windows does
  28. /// recognise file names with the "\" replaced by "/" (at least in system calls,
  29. /// if not the command line).
  30. ///
  31. /// \param name Name to be substituted
  32. void normalizeSlash(std::string& name);
  33. /// \brief Trim Leading and Trailing Spaces
  34. ///
  35. /// Returns a copy of the input string but with any leading or trailing spaces
  36. /// or tabs removed.
  37. ///
  38. /// \param instring Input string to modify
  39. ///
  40. /// \return String with leading and trailing spaces removed
  41. std::string trim(const std::string& instring);
  42. /// \brief Split String into Tokens
  43. ///
  44. /// Splits a string into tokens (the tokens being delimited by one or more of
  45. /// the delimiter characters) and returns the tokens in a vector array. Note
  46. /// that adjacent delimiters are considered to be a single delimiter.
  47. ///
  48. /// Special cases are:
  49. /// -# The empty string is considered to be zero tokens.
  50. /// -# A string comprising nothing but delimiters is considered to be zero
  51. /// tokens.
  52. ///
  53. /// The reasoning behind this is that the string can be thought of as having
  54. /// invisible leading and trailing delimiter characters. Therefore both cases
  55. /// reduce to a set of contiguous delimiters, which are considered a single
  56. /// delimiter (so getting rid of the string).
  57. ///
  58. /// We could use Boost for this, but this (simple) function eliminates one
  59. /// dependency in the code.
  60. ///
  61. /// \param text String to be split. Passed by value as the internal copy is
  62. /// altered during the processing.
  63. /// \param delim Delimiter characters
  64. ///
  65. /// \return Vector of tokens.
  66. std::vector<std::string> tokens(const std::string text,
  67. const std::string& delim = std::string(" \t\n"));
  68. /// \brief Uppercase Character
  69. ///
  70. /// Used in uppercase() to pass as an argument to std::transform(). The
  71. /// function std::toupper() can't be used as it takes an "int" as its argument;
  72. /// this confuses the template expansion mechanism because defererencing a
  73. /// string::iterator returns a char.
  74. ///
  75. /// \param chr Character to be upper-cased.
  76. ///
  77. /// \return Uppercase version of the argument
  78. inline char toUpper(char chr) {
  79. return static_cast<char>(std::toupper(static_cast<int>(chr)));
  80. }
  81. /// \brief Uppercase String
  82. ///
  83. /// A convenience function to uppercase a string.
  84. ///
  85. /// \param text String to be upper-cased.
  86. inline void uppercase(std::string& text) {
  87. std::transform(text.begin(), text.end(), text.begin(),
  88. isc::strutil::toUpper);
  89. }
  90. /// \brief Lowercase Character
  91. ///
  92. /// Used in lowercase() to pass as an argument to std::transform(). The
  93. /// function std::tolower() can't be used as it takes an "int" as its argument;
  94. /// this confuses the template expansion mechanism because defererencing a
  95. /// string::iterator returns a char.
  96. ///
  97. /// \param chr Character to be lower-cased.
  98. ///
  99. /// \return Lowercase version of the argument
  100. inline char toLower(char chr) {
  101. return static_cast<char>(std::tolower(static_cast<int>(chr)));
  102. }
  103. /// \brief Lowercase String
  104. ///
  105. /// A convenience function to lowercase a string
  106. ///
  107. /// \param text String to be lower-cased.
  108. inline void lowercase(std::string& text) {
  109. std::transform(text.begin(), text.end(), text.begin(),
  110. isc::strutil::toLower);
  111. }
  112. /// \brief Apply Formatting
  113. ///
  114. /// Given a printf-style format string containing only "%s" place holders
  115. /// (others are ignored) and a vector of strings, this produces a single string
  116. /// with the placeholders replaced.
  117. ///
  118. /// \param format Format string
  119. /// \param args Vector of argument strings
  120. ///
  121. /// \return Resultant string
  122. std::string format(const std::string& format,
  123. const std::vector<std::string>& args);
  124. } // namespace strutil
  125. } // namespace isc
  126. #endif // __STRUTIL_H