string_util.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2011 Google 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. #ifndef DOMAIN_REGISTRY_PRIVATE_STRING_UTIL_H_
  17. #define DOMAIN_REGISTRY_PRIVATE_STRING_UTIL_H_
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include "tsk_assert.h"
  21. static const char kUpperLowerDistance = 'A' - 'a';
  22. #if _WINDOWS
  23. #define __inline__ __inline
  24. #endif
  25. static __inline__ int IsWildcardComponent(const char* component) {
  26. if (component[0] == '*') {
  27. return 1;
  28. }
  29. return 0;
  30. }
  31. static __inline__ int IsExceptionComponent(const char* component) {
  32. if (component[0] == '!') {
  33. return 1;
  34. }
  35. return 0;
  36. }
  37. static __inline__ int IsInvalidComponent(const char* component) {
  38. if (component == NULL ||
  39. component[0] == 0 ||
  40. IsExceptionComponent(component) ||
  41. IsWildcardComponent(component)) {
  42. return 1;
  43. }
  44. return 0;
  45. }
  46. static __inline__ void ReplaceChar(char* value, char old, char newval) {
  47. while ((value = strchr(value, old)) != NULL) {
  48. *value = newval;
  49. ++value;
  50. }
  51. }
  52. static __inline__ void ToLowerASCII(char* buf, const char* end) {
  53. for (; buf < end; ++buf) {
  54. char c = *buf;
  55. if (c >= 'A' && c <= 'Z') {
  56. *buf = c - kUpperLowerDistance;
  57. }
  58. }
  59. }
  60. static __inline__ int HostnamePartCmp(const char *a, const char *b) {
  61. /*
  62. * Optimization: do not invoke strcmp() unless the first characters
  63. * in each string match. Since we are performing a binary search, we
  64. * expect most invocations to strcmp to not have matching arguments,
  65. * and thus not invoke strcmp. This reduces overall runtime by 5-10%
  66. * on a Linux laptop running a -O2 optimized build.
  67. */
  68. int ret = *(unsigned char *)a - *(unsigned char *)b;
  69. /*
  70. * NOTE: we could invoke strcmp on a+1,b+1 if we are
  71. * certain that neither a nor b are the empty string. For now we
  72. * take the more conservative approach.
  73. */
  74. if (ret == 0) return strcmp(a, b);
  75. return ret;
  76. }
  77. #endif /* DOMAIN_REGISTRY_PRIVATE_STRING_UTIL_H_ */