make_fast_names.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * @file make_fast_names.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stddef.h>
  30. #include <misc/debug.h>
  31. #include <misc/merge.h>
  32. #include <misc/balloc.h>
  33. #include <ncd/NCDStringIndex.h>
  34. // Input parameters:
  35. // #define NAMES_PARAM_NAME
  36. // #define NAMES_PARAM_TYPE struct instance
  37. // #define NAMES_PARAM_MEMBER_DYNAMIC_NAMES dynamic_names
  38. // #define NAMES_PARAM_MEMBER_STATIC_NAMES static_names
  39. // #define NAMES_PARAM_MEMBER_NUM_NAMES num_names
  40. // #define NAMES_PARAM_NUM_STATIC_NAMES 10
  41. #define MakeFastNames_count_names MERGE(NAMES_PARAM_NAME, _count_names)
  42. #define MakeFastNames_add_name MERGE(NAMES_PARAM_NAME, _add_name)
  43. #define MakeFastNames_InitNames MERGE(NAMES_PARAM_NAME, _InitNames)
  44. #define MakeFastNames_FreeNames MERGE(NAMES_PARAM_NAME, _FreeNames)
  45. #define MakeFastNames_GetNames MERGE(NAMES_PARAM_NAME, _GetNames)
  46. static size_t MakeFastNames_count_names (const char *str, size_t str_len)
  47. {
  48. size_t count = 1;
  49. while (str_len > 0) {
  50. if (*str == '.') {
  51. count++;
  52. }
  53. str++;
  54. str_len--;
  55. }
  56. return count;
  57. }
  58. static int MakeFastNames_add_name (NAMES_PARAM_TYPE *o, NCDStringIndex *string_index, const char *str, size_t str_len, const char *remain, size_t remain_len)
  59. {
  60. ASSERT(str)
  61. ASSERT(!!o->NAMES_PARAM_MEMBER_DYNAMIC_NAMES == (o->NAMES_PARAM_MEMBER_NUM_NAMES > NAMES_PARAM_NUM_STATIC_NAMES))
  62. NCD_string_id_t id = NCDStringIndex_GetBin(string_index, str, str_len);
  63. if (id < 0) {
  64. return 0;
  65. }
  66. if (o->NAMES_PARAM_MEMBER_NUM_NAMES < NAMES_PARAM_NUM_STATIC_NAMES) {
  67. o->NAMES_PARAM_MEMBER_STATIC_NAMES[o->NAMES_PARAM_MEMBER_NUM_NAMES++] = id;
  68. return 1;
  69. }
  70. if (o->NAMES_PARAM_MEMBER_NUM_NAMES == NAMES_PARAM_NUM_STATIC_NAMES) {
  71. size_t num_more = (!remain ? 0 : MakeFastNames_count_names(remain, remain_len));
  72. size_t num_all = o->NAMES_PARAM_MEMBER_NUM_NAMES + 1 + num_more;
  73. if (!(o->NAMES_PARAM_MEMBER_DYNAMIC_NAMES = BAllocArray(num_all, sizeof(o->NAMES_PARAM_MEMBER_DYNAMIC_NAMES[0])))) {
  74. return 0;
  75. }
  76. memcpy(o->NAMES_PARAM_MEMBER_DYNAMIC_NAMES, o->NAMES_PARAM_MEMBER_STATIC_NAMES, NAMES_PARAM_NUM_STATIC_NAMES * sizeof(o->NAMES_PARAM_MEMBER_DYNAMIC_NAMES[0]));
  77. }
  78. o->NAMES_PARAM_MEMBER_DYNAMIC_NAMES[o->NAMES_PARAM_MEMBER_NUM_NAMES++] = id;
  79. return 1;
  80. }
  81. static int MakeFastNames_InitNames (NAMES_PARAM_TYPE *o, NCDStringIndex *string_index, const char *str, size_t str_len)
  82. {
  83. ASSERT(str)
  84. o->NAMES_PARAM_MEMBER_NUM_NAMES = 0;
  85. o->NAMES_PARAM_MEMBER_DYNAMIC_NAMES = NULL;
  86. size_t i = 0;
  87. while (i < str_len) {
  88. if (str[i] == '.') {
  89. if (!MakeFastNames_add_name(o, string_index, str, i, str + (i + 1), str_len - (i + 1))) {
  90. goto fail;
  91. }
  92. str += i + 1;
  93. str_len -= i + 1;
  94. i = 0;
  95. continue;
  96. }
  97. i++;
  98. }
  99. if (!MakeFastNames_add_name(o, string_index, str, i, NULL, 0)) {
  100. goto fail;
  101. }
  102. return 1;
  103. fail:
  104. BFree(o->NAMES_PARAM_MEMBER_DYNAMIC_NAMES);
  105. return 0;
  106. }
  107. static void MakeFastNames_FreeNames (NAMES_PARAM_TYPE *o)
  108. {
  109. if (o->NAMES_PARAM_MEMBER_DYNAMIC_NAMES) {
  110. BFree(o->NAMES_PARAM_MEMBER_DYNAMIC_NAMES);
  111. }
  112. }
  113. static NCD_string_id_t * MakeFastNames_GetNames (NAMES_PARAM_TYPE *o)
  114. {
  115. ASSERT(o->NAMES_PARAM_MEMBER_NUM_NAMES > 0)
  116. return (o->NAMES_PARAM_MEMBER_DYNAMIC_NAMES ? o->NAMES_PARAM_MEMBER_DYNAMIC_NAMES : o->NAMES_PARAM_MEMBER_STATIC_NAMES);
  117. }
  118. #undef MakeFastNames_count_names
  119. #undef MakeFastNames_add_name
  120. #undef MakeFastNames_InitNames
  121. #undef MakeFastNames_FreeNames
  122. #undef MakeFastNames_GetNames
  123. #undef NAMES_PARAM_NAME
  124. #undef NAMES_PARAM_TYPE
  125. #undef NAMES_PARAM_MEMBER_DYNAMIC_NAMES
  126. #undef NAMES_PARAM_MEMBER_STATIC_NAMES
  127. #undef NAMES_PARAM_MEMBER_NUM_NAMES
  128. #undef NAMES_PARAM_NUM_STATIC_NAMES