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

memutil.h 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. {
  68. ABSL_NAMESPACE_BEGIN
  69. namespace strings_internal
  70. {
  71. inline char* memcat(char* dest, size_t destlen, const char* src, size_t srclen)
  72. {
  73. return reinterpret_cast<char*>(memcpy(dest + destlen, src, srclen));
  74. }
  75. int memcasecmp(const char* s1, const char* s2, size_t len);
  76. char* memdup(const char* s, size_t slen);
  77. char* memrchr(const char* s, int c, size_t slen);
  78. size_t memspn(const char* s, size_t slen, const char* accept);
  79. size_t memcspn(const char* s, size_t slen, const char* reject);
  80. char* mempbrk(const char* s, size_t slen, const char* accept);
  81. // This is for internal use only. Don't call this directly
  82. template<bool case_sensitive>
  83. const char* int_memmatch(const char* haystack, size_t haylen, const char* needle, size_t neelen)
  84. {
  85. if (0 == neelen)
  86. {
  87. return haystack; // even if haylen is 0
  88. }
  89. const char* hayend = haystack + haylen;
  90. const char* needlestart = needle;
  91. const char* needleend = needlestart + neelen;
  92. for (; haystack < hayend; ++haystack)
  93. {
  94. char hay = case_sensitive ? *haystack : absl::ascii_tolower(static_cast<unsigned char>(*haystack));
  95. char nee = case_sensitive ? *needle : absl::ascii_tolower(static_cast<unsigned char>(*needle));
  96. if (hay == nee)
  97. {
  98. if (++needle == needleend)
  99. {
  100. return haystack + 1 - neelen;
  101. }
  102. }
  103. else if (needle != needlestart)
  104. {
  105. // must back up haystack in case a prefix matched (find "aab" in "aaab")
  106. haystack -= needle - needlestart; // for loop will advance one more
  107. needle = needlestart;
  108. }
  109. }
  110. return nullptr;
  111. }
  112. // These are the guys you can call directly
  113. inline const char* memstr(const char* phaystack, size_t haylen, const char* pneedle)
  114. {
  115. return int_memmatch<true>(phaystack, haylen, pneedle, strlen(pneedle));
  116. }
  117. inline const char* memcasestr(const char* phaystack, size_t haylen, const char* pneedle)
  118. {
  119. return int_memmatch<false>(phaystack, haylen, pneedle, strlen(pneedle));
  120. }
  121. inline const char* memmem(const char* phaystack, size_t haylen, const char* pneedle, size_t needlelen)
  122. {
  123. return int_memmatch<true>(phaystack, haylen, pneedle, needlelen);
  124. }
  125. inline const char* memcasemem(const char* phaystack, size_t haylen, const char* pneedle, size_t needlelen)
  126. {
  127. return int_memmatch<false>(phaystack, haylen, pneedle, needlelen);
  128. }
  129. // This is significantly faster for case-sensitive matches with very
  130. // few possible matches. See unit test for benchmarks.
  131. const char* memmatch(const char* phaystack, size_t haylen, const char* pneedle, size_t neelen);
  132. } // namespace strings_internal
  133. ABSL_NAMESPACE_END
  134. } // namespace absl
  135. #endif // ABSL_STRINGS_INTERNAL_MEMUTIL_H_