strutil.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (C) 2011 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. #include <numeric>
  15. #include <string.h>
  16. #include <strutil.h>
  17. using namespace std;
  18. namespace isc {
  19. namespace strutil {
  20. // Normalize slashes
  21. void
  22. normalizeSlash(std::string& name) {
  23. if (!name.empty()) {
  24. size_t pos = 0;
  25. while ((pos = name.find('\\', pos)) != std::string::npos) {
  26. name[pos] = '/';
  27. }
  28. }
  29. }
  30. // Trim String
  31. string
  32. trim(const string& instring) {
  33. static const char* blanks = " \t\n";
  34. string retstring = "";
  35. if (!instring.empty()) {
  36. // Search for first non-blank character in the string
  37. size_t first = instring.find_first_not_of(blanks);
  38. if (first != string::npos) {
  39. // String not all blanks, so look for last character
  40. size_t last = instring.find_last_not_of(blanks);
  41. // Extract the trimmed substring
  42. retstring = instring.substr(first, (last - first + 1));
  43. }
  44. }
  45. return (retstring);
  46. }
  47. // Tokenise string. As noted in the header, this is locally written to avoid
  48. // another dependency on a Boost library.
  49. vector<string>
  50. tokens(const std::string& text, const std::string& delim) {
  51. vector<string> result;
  52. // Search for the first non-delimiter character
  53. size_t start = text.find_first_not_of(delim);
  54. while (start != string::npos) {
  55. // Non-delimiter found, look for next delimiter
  56. size_t end = text.find_first_of(delim, start);
  57. if (end != string::npos) {
  58. // Delimiter found, so extract string & search for start of next
  59. // non-delimiter segment.
  60. result.push_back(text.substr(start, (end - start)));
  61. start = text.find_first_not_of(delim, end);
  62. } else {
  63. // End of string found, extract rest of string and flag to exit
  64. result.push_back(text.substr(start));
  65. start = string::npos;
  66. }
  67. }
  68. return (result);
  69. }
  70. // Local function to pass to accumulate() for summing up string lengths.
  71. namespace {
  72. size_t
  73. lengthSum(string::size_type curlen, const string& cur_string) {
  74. return (curlen + cur_string.size());
  75. }
  76. }
  77. // Provide printf-style formatting.
  78. std::string
  79. format(const std::string& format, const std::vector<std::string>& args) {
  80. static const string flag = "%s";
  81. // Initialize return string. To speed things up, we'll reserve an
  82. // appropriate amount of space - current string size, plus length of all
  83. // the argument strings, less two characters for each argument (the %s in
  84. // the format string is being replaced).
  85. string result;
  86. size_t length = accumulate(args.begin(), args.end(), format.size(),
  87. lengthSum) - (args.size() * flag.size());
  88. result.reserve(length);
  89. // Iterate through replacing all tokens
  90. result = format;
  91. size_t tokenpos = 0; // Position of last token replaced
  92. int i = 0; // Index into argument array
  93. while ((i < args.size()) && (tokenpos != string::npos)) {
  94. tokenpos = result.find(flag, tokenpos);
  95. if (tokenpos != string::npos) {
  96. result.replace(tokenpos, flag.size(), args[i++]);
  97. }
  98. }
  99. return (result);
  100. }
  101. } // namespace log
  102. } // namespace isc