You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/stringutils.h"
  28. #include "talk/base/common.h"
  29. namespace talk_base {
  30. bool memory_check(const void* memory, int c, size_t count) {
  31. const char* char_memory = static_cast<const char*>(memory);
  32. char char_c = static_cast<char>(c);
  33. for (size_t i = 0; i < count; ++i) {
  34. if (char_memory[i] != char_c) {
  35. return false;
  36. }
  37. }
  38. return true;
  39. }
  40. bool string_match(const char* target, const char* pattern) {
  41. while (*pattern) {
  42. if (*pattern == '*') {
  43. if (!*++pattern) {
  44. return true;
  45. }
  46. while (*target) {
  47. if ((toupper(*pattern) == toupper(*target))
  48. && string_match(target + 1, pattern + 1)) {
  49. return true;
  50. }
  51. ++target;
  52. }
  53. return false;
  54. } else {
  55. if (toupper(*pattern) != toupper(*target)) {
  56. return false;
  57. }
  58. ++target;
  59. ++pattern;
  60. }
  61. }
  62. return !*target;
  63. }
  64. #ifdef WIN32
  65. int ascii_string_compare(const wchar_t* s1, const char* s2, size_t n,
  66. CharacterTransformation transformation) {
  67. wchar_t c1, c2;
  68. while (true) {
  69. if (n-- == 0) return 0;
  70. c1 = transformation(*s1);
  71. // Double check that characters are not UTF-8
  72. ASSERT(static_cast<unsigned char>(*s2) < 128);
  73. // Note: *s2 gets implicitly promoted to wchar_t
  74. c2 = transformation(*s2);
  75. if (c1 != c2) return (c1 < c2) ? -1 : 1;
  76. if (!c1) return 0;
  77. ++s1;
  78. ++s2;
  79. }
  80. }
  81. size_t asccpyn(wchar_t* buffer, size_t buflen,
  82. const char* source, size_t srclen) {
  83. if (buflen <= 0)
  84. return 0;
  85. if (srclen == SIZE_UNKNOWN) {
  86. srclen = strlenn(source, buflen - 1);
  87. } else if (srclen >= buflen) {
  88. srclen = buflen - 1;
  89. }
  90. #if _DEBUG
  91. // Double check that characters are not UTF-8
  92. for (size_t pos = 0; pos < srclen; ++pos)
  93. ASSERT(static_cast<unsigned char>(source[pos]) < 128);
  94. #endif // _DEBUG
  95. std::copy(source, source + srclen, buffer);
  96. buffer[srclen] = 0;
  97. return srclen;
  98. }
  99. #endif // WIN32
  100. void replace_substrs(const char *search,
  101. size_t search_len,
  102. const char *replace,
  103. size_t replace_len,
  104. std::string *s) {
  105. size_t pos = 0;
  106. while ((pos = s->find(search, pos, search_len)) != std::string::npos) {
  107. s->replace(pos, search_len, replace, replace_len);
  108. pos += replace_len;
  109. }
  110. }
  111. bool starts_with(const char *s1, const char *s2) {
  112. return strncmp(s1, s2, strlen(s2)) == 0;
  113. }
  114. bool ends_with(const char *s1, const char *s2) {
  115. size_t s1_length = strlen(s1);
  116. size_t s2_length = strlen(s2);
  117. if (s2_length > s1_length) {
  118. return false;
  119. }
  120. const char* start = s1 + (s1_length - s2_length);
  121. return strncmp(start, s2, s2_length) == 0;
  122. }
  123. static const char kWhitespace[] = " \n\r\t";
  124. std::string string_trim(const std::string& s) {
  125. std::string::size_type first = s.find_first_not_of(kWhitespace);
  126. std::string::size_type last = s.find_last_not_of(kWhitespace);
  127. if (first == std::string::npos || last == std::string::npos) {
  128. return std::string("");
  129. }
  130. return s.substr(first, last - first + 1);
  131. }
  132. } // namespace talk_base