tree_data_keycmp.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright [2021] JD.com, 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 <ctype.h>
  17. static inline int stricmp(const char *p, const char *q)
  18. {
  19. while (toupper(*(unsigned char *)p) == toupper(*(unsigned char *)q)) {
  20. if (*p == '\0') {
  21. return 0;
  22. }
  23. p += 1;
  24. q += 1;
  25. }
  26. return toupper(*(unsigned char *)p) - toupper(*(unsigned char *)q);
  27. }
  28. static inline int strincmp(const char *p, const char *q, size_t n)
  29. {
  30. while (n > 0) {
  31. int diff = toupper(*(unsigned char *)p) -
  32. toupper(*(unsigned char *)q);
  33. if (diff != 0) {
  34. return diff;
  35. } else if (*p == '\0') {
  36. return 0;
  37. }
  38. p += 1;
  39. q += 1;
  40. n -= 1;
  41. }
  42. return 0;
  43. }
  44. static inline int stricoll(const char *p, const char *q)
  45. {
  46. char p_buf[256];
  47. char q_buf[256];
  48. size_t p_len = strlen(p);
  49. size_t q_len = strlen(q);
  50. char *p_dst = p_buf;
  51. char *q_dst = q_buf;
  52. int i;
  53. if (p_len >= sizeof(p_buf)) {
  54. p_dst = new char[p_len + 1];
  55. }
  56. if (q_len >= sizeof(q_buf)) {
  57. q_dst = new char[q_len + 1];
  58. }
  59. for (i = 0; p[i] != '\0'; i++) {
  60. p_dst[i] = toupper(p[i] & 0xFF);
  61. }
  62. p_dst[i] = '\0';
  63. for (i = 0; q[i] != '\0'; i++) {
  64. q_dst[i] = toupper(q[i] & 0xFF);
  65. }
  66. q_dst[i] = '\0';
  67. int diff = strcoll(p_dst, q_dst);
  68. if (p_dst != p_buf) {
  69. delete[] p_dst;
  70. }
  71. if (q_dst != q_buf) {
  72. delete[] q_dst;
  73. }
  74. return diff;
  75. }