memutil.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Copyright 2017 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // These routines provide mem versions of standard C string routines,
  17. // such as strpbrk. They function exactly the same as the str versions,
  18. // so if you wonder what they are, replace the word "mem" by
  19. // "str" and check out the man page. I could return void*, as the
  20. // strutil.h mem*() routines tend to do, but I return char* instead
  21. // since this is by far the most common way these functions are called.
  22. //
  23. // The difference between the mem and str versions is the mem version
  24. // takes a pointer and a length, rather than a '\0'-terminated string.
  25. // The memcase* routines defined here assume the locale is "C"
  26. // (they use absl::ascii_tolower instead of tolower).
  27. //
  28. // These routines are based on the BSD library.
  29. //
  30. // Here's a list of routines from string.h, and their mem analogues.
  31. // Functions in lowercase are defined in string.h; those in UPPERCASE
  32. // are defined here:
  33. //
  34. // strlen --
  35. // strcat strncat MEMCAT
  36. // strcpy strncpy memcpy
  37. // -- memccpy (very cool function, btw)
  38. // -- memmove
  39. // -- memset
  40. // strcmp strncmp memcmp
  41. // strcasecmp strncasecmp MEMCASECMP
  42. // strchr memchr
  43. // strcoll --
  44. // strxfrm --
  45. // strdup strndup MEMDUP
  46. // strrchr MEMRCHR
  47. // strspn MEMSPN
  48. // strcspn MEMCSPN
  49. // strpbrk MEMPBRK
  50. // strstr MEMSTR MEMMEM
  51. // (g)strcasestr MEMCASESTR MEMCASEMEM
  52. // strtok --
  53. // strprefix MEMPREFIX (strprefix is from strutil.h)
  54. // strcaseprefix MEMCASEPREFIX (strcaseprefix is from strutil.h)
  55. // strsuffix MEMSUFFIX (strsuffix is from strutil.h)
  56. // strcasesuffix MEMCASESUFFIX (strcasesuffix is from strutil.h)
  57. // -- MEMIS
  58. // -- MEMCASEIS
  59. // strcount MEMCOUNT (strcount is from strutil.h)
  60. #ifndef ABSL_STRINGS_INTERNAL_MEMUTIL_H_
  61. #define ABSL_STRINGS_INTERNAL_MEMUTIL_H_
  62. #include <cstddef>
  63. #include <cstring>
  64. #include "absl/base/port.h" // disable some warnings on Windows
  65. #include "absl/strings/ascii.h" // for absl::ascii_tolower
  66. namespace absl {
  67. ABSL_NAMESPACE_BEGIN
  68. namespace strings_internal {
  69. inline char* memcat(char* dest, size_t destlen, const char* src,
  70. size_t srclen) {
  71. return reinterpret_cast<char*>(memcpy(dest + destlen, src, srclen));
  72. }
  73. int memcasecmp(const char* s1, const char* s2, size_t len);
  74. char* memdup(const char* s, size_t slen);
  75. char* memrchr(const char* s, int c, size_t slen);
  76. size_t memspn(const char* s, size_t slen, const char* accept);
  77. size_t memcspn(const char* s, size_t slen, const char* reject);
  78. char* mempbrk(const char* s, size_t slen, const char* accept);
  79. // This is for internal use only. Don't call this directly
  80. template <bool case_sensitive>
  81. const char* int_memmatch(const char* haystack, size_t haylen,
  82. const char* needle, size_t neelen) {
  83. if (0 == neelen) {
  84. return haystack; // even if haylen is 0
  85. }
  86. const char* hayend = haystack + haylen;
  87. const char* needlestart = needle;
  88. const char* needleend = needlestart + neelen;
  89. for (; haystack < hayend; ++haystack) {
  90. char hay = case_sensitive
  91. ? *haystack
  92. : absl::ascii_tolower(static_cast<unsigned char>(*haystack));
  93. char nee = case_sensitive
  94. ? *needle
  95. : absl::ascii_tolower(static_cast<unsigned char>(*needle));
  96. if (hay == nee) {
  97. if (++needle == needleend) {
  98. return haystack + 1 - neelen;
  99. }
  100. } else if (needle != needlestart) {
  101. // must back up haystack in case a prefix matched (find "aab" in "aaab")
  102. haystack -= needle - needlestart; // for loop will advance one more
  103. needle = needlestart;
  104. }
  105. }
  106. return nullptr;
  107. }
  108. // These are the guys you can call directly
  109. inline const char* memstr(const char* phaystack, size_t haylen,
  110. const char* pneedle) {
  111. return int_memmatch<true>(phaystack, haylen, pneedle, strlen(pneedle));
  112. }
  113. inline const char* memcasestr(const char* phaystack, size_t haylen,
  114. const char* pneedle) {
  115. return int_memmatch<false>(phaystack, haylen, pneedle, strlen(pneedle));
  116. }
  117. inline const char* memmem(const char* phaystack, size_t haylen,
  118. const char* pneedle, size_t needlelen) {
  119. return int_memmatch<true>(phaystack, haylen, pneedle, needlelen);
  120. }
  121. inline const char* memcasemem(const char* phaystack, size_t haylen,
  122. const char* pneedle, size_t needlelen) {
  123. return int_memmatch<false>(phaystack, haylen, pneedle, needlelen);
  124. }
  125. // This is significantly faster for case-sensitive matches with very
  126. // few possible matches. See unit test for benchmarks.
  127. const char* memmatch(const char* phaystack, size_t haylen, const char* pneedle,
  128. size_t neelen);
  129. } // namespace strings_internal
  130. ABSL_NAMESPACE_END
  131. } // namespace absl
  132. #endif // ABSL_STRINGS_INTERNAL_MEMUTIL_H_