char_utils.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**************************************************************************/
  2. /* char_utils.hpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "char_range.inc.hpp"
  32. namespace godot {
  33. #define BSEARCH_CHAR_RANGE(m_array) \
  34. int low = 0; \
  35. int high = sizeof(m_array) / sizeof(m_array[0]) - 1; \
  36. int middle = (low + high) / 2; \
  37. \
  38. while (low <= high) { \
  39. if (p_char < m_array[middle].start) { \
  40. high = middle - 1; \
  41. } else if (p_char > m_array[middle].end) { \
  42. low = middle + 1; \
  43. } else { \
  44. return true; \
  45. } \
  46. \
  47. middle = (low + high) / 2; \
  48. } \
  49. \
  50. return false
  51. constexpr bool is_unicode_identifier_start(char32_t p_char) {
  52. BSEARCH_CHAR_RANGE(xid_start);
  53. }
  54. constexpr bool is_unicode_identifier_continue(char32_t p_char) {
  55. BSEARCH_CHAR_RANGE(xid_continue);
  56. }
  57. constexpr bool is_unicode_upper_case(char32_t p_char) {
  58. BSEARCH_CHAR_RANGE(uppercase_letter);
  59. }
  60. constexpr bool is_unicode_lower_case(char32_t p_char) {
  61. BSEARCH_CHAR_RANGE(lowercase_letter);
  62. }
  63. constexpr bool is_unicode_letter(char32_t p_char) {
  64. BSEARCH_CHAR_RANGE(unicode_letter);
  65. }
  66. #undef BSEARCH_CHAR_RANGE
  67. constexpr bool is_ascii_upper_case(char32_t p_char) {
  68. return (p_char >= 'A' && p_char <= 'Z');
  69. }
  70. constexpr bool is_ascii_lower_case(char32_t p_char) {
  71. return (p_char >= 'a' && p_char <= 'z');
  72. }
  73. constexpr bool is_digit(char32_t p_char) {
  74. return (p_char >= '0' && p_char <= '9');
  75. }
  76. constexpr bool is_hex_digit(char32_t p_char) {
  77. return (is_digit(p_char) || (p_char >= 'a' && p_char <= 'f') || (p_char >= 'A' && p_char <= 'F'));
  78. }
  79. constexpr bool is_binary_digit(char32_t p_char) {
  80. return (p_char == '0' || p_char == '1');
  81. }
  82. constexpr bool is_ascii_alphabet_char(char32_t p_char) {
  83. return (p_char >= 'a' && p_char <= 'z') || (p_char >= 'A' && p_char <= 'Z');
  84. }
  85. constexpr bool is_ascii_alphanumeric_char(char32_t p_char) {
  86. return (p_char >= 'a' && p_char <= 'z') || (p_char >= 'A' && p_char <= 'Z') || (p_char >= '0' && p_char <= '9');
  87. }
  88. constexpr bool is_ascii_identifier_char(char32_t p_char) {
  89. return (p_char >= 'a' && p_char <= 'z') || (p_char >= 'A' && p_char <= 'Z') || (p_char >= '0' && p_char <= '9') || p_char == '_';
  90. }
  91. constexpr bool is_symbol(char32_t p_char) {
  92. return p_char != '_' && ((p_char >= '!' && p_char <= '/') || (p_char >= ':' && p_char <= '@') || (p_char >= '[' && p_char <= '`') || (p_char >= '{' && p_char <= '~') || p_char == '\t' || p_char == ' ');
  93. }
  94. constexpr bool is_control(char32_t p_char) {
  95. return (p_char <= 0x001f) || (p_char >= 0x007f && p_char <= 0x009f);
  96. }
  97. constexpr bool is_whitespace(char32_t p_char) {
  98. return (p_char == ' ') || (p_char == 0x00a0) || (p_char == 0x1680) || (p_char >= 0x2000 && p_char <= 0x200b) || (p_char == 0x202f) || (p_char == 0x205f) || (p_char == 0x3000) || (p_char == 0x2028) || (p_char == 0x2029) || (p_char >= 0x0009 && p_char <= 0x000d) || (p_char == 0x0085);
  99. }
  100. constexpr bool is_linebreak(char32_t p_char) {
  101. return (p_char >= 0x000a && p_char <= 0x000d) || (p_char == 0x0085) || (p_char == 0x2028) || (p_char == 0x2029);
  102. }
  103. constexpr bool is_punct(char32_t p_char) {
  104. return (p_char >= ' ' && p_char <= '/') || (p_char >= ':' && p_char <= '@') || (p_char >= '[' && p_char <= '^') || (p_char == '`') || (p_char >= '{' && p_char <= '~') || (p_char >= 0x2000 && p_char <= 0x206f) || (p_char >= 0x3000 && p_char <= 0x303f);
  105. }
  106. constexpr bool is_underscore(char32_t p_char) {
  107. return (p_char == '_');
  108. }
  109. } // namespace godot