String.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright 2016-present Facebook, Inc.
  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. * http://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. #include <folly/portability/String.h>
  17. #if !FOLLY_HAVE_MEMRCHR
  18. extern "C" void* memrchr(const void* s, int c, size_t n) {
  19. for (auto p = ((const char*)s) + n - 1; p >= (const char*)s; p--) {
  20. if (*p == (char)c) {
  21. return (void*)p;
  22. }
  23. }
  24. return nullptr;
  25. }
  26. #endif
  27. #if defined(_WIN32) || defined(__FreeBSD__)
  28. extern "C" char* strndup(const char* a, size_t len) {
  29. auto neededLen = strlen(a);
  30. if (neededLen > len) {
  31. neededLen = len;
  32. }
  33. char* buf = (char*)malloc((neededLen + 1) * sizeof(char));
  34. if (!buf) {
  35. return nullptr;
  36. }
  37. memcpy(buf, a, neededLen);
  38. buf[neededLen] = '\0';
  39. return buf;
  40. }
  41. #endif
  42. #ifdef _WIN32
  43. extern "C" {
  44. void bzero(void* s, size_t n) {
  45. memset(s, 0, n);
  46. }
  47. int strcasecmp(const char* a, const char* b) {
  48. return _stricmp(a, b);
  49. }
  50. int strncasecmp(const char* a, const char* b, size_t c) {
  51. return _strnicmp(a, b, c);
  52. }
  53. char* strtok_r(char* str, char const* delim, char** ctx) {
  54. return strtok_s(str, delim, ctx);
  55. }
  56. }
  57. #endif