UnicodeRange.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "precompiled.h"
  29. #include "UnicodeRange.h"
  30. namespace Rml {
  31. namespace Core {
  32. UnicodeRange::UnicodeRange()
  33. {
  34. min_codepoint = UINT_MAX;
  35. max_codepoint = UINT_MAX;
  36. }
  37. UnicodeRange::UnicodeRange(int _min_codepoint, int _max_codepoint)
  38. {
  39. min_codepoint = _min_codepoint;
  40. max_codepoint = _max_codepoint;
  41. RMLUI_ASSERT(min_codepoint <= max_codepoint);
  42. }
  43. // Initialises the range from a unicode range in string form.
  44. bool UnicodeRange::Initialise(const String& unicode_range)
  45. {
  46. // Check for a 'U+' at the start.
  47. if (unicode_range.size() < 2 ||
  48. unicode_range[0] != 'U' ||
  49. unicode_range[1] != '+')
  50. return false;
  51. // Check if there's a '-' sign; if so, we've got a range.
  52. String::size_type separator_index = unicode_range.find("-", 2);
  53. if (separator_index != String::npos)
  54. {
  55. const char* end = unicode_range.c_str() + separator_index;
  56. min_codepoint = strtoul(unicode_range.c_str() + 2, (char **) &end, 16);
  57. end = unicode_range.c_str() + unicode_range.size();
  58. max_codepoint = strtoul(unicode_range.c_str() + separator_index + 1, (char **) &end, 16);
  59. return min_codepoint <= max_codepoint;
  60. }
  61. // No range! Check if we have any wildcards.
  62. String::size_type wildcard_index = unicode_range.find("?", 2);
  63. if (wildcard_index != String::npos)
  64. {
  65. String range_min(unicode_range.c_str() + 2, unicode_range.c_str() + wildcard_index);
  66. String range_max(range_min);
  67. for (String::size_type i = 0; i < unicode_range.size() - wildcard_index; ++i)
  68. {
  69. range_min += "0";
  70. range_max += "F";
  71. }
  72. const char* end = range_min.c_str() + range_min.size();
  73. min_codepoint = strtoul(range_min.c_str(), (char**) &end, 16);
  74. end = range_max.c_str() + range_max.size();
  75. max_codepoint = strtoul(range_max.c_str(), (char**) &end, 16);
  76. return true;
  77. }
  78. const char* end = unicode_range.c_str() + unicode_range.size();
  79. min_codepoint = strtoul(unicode_range.c_str() + 2, (char**) &end, 16);
  80. max_codepoint = min_codepoint;
  81. return true;
  82. }
  83. // Builds up a list of unicode ranges from a comma-separated list of unicode ranges in string form.
  84. bool UnicodeRange::BuildList(UnicodeRangeList& list, const String& unicode_range)
  85. {
  86. StringList unicode_ranges;
  87. StringUtilities::ExpandString(unicode_ranges, unicode_range);
  88. for (size_t i = 0; i < unicode_ranges.size(); ++i)
  89. {
  90. UnicodeRange range;
  91. if (!range.Initialise(unicode_ranges[i]))
  92. return false;
  93. list.push_back(range);
  94. }
  95. // Collapse contiguous ranges.
  96. for (size_t i = 0; i < list.size(); ++i)
  97. {
  98. size_t j = i + 1;
  99. while (j < list.size())
  100. {
  101. if (list[i].IsContiguous(list[j]))
  102. {
  103. list[i] = list[i].Join(list[j]);
  104. list.erase(list.begin() + j);
  105. }
  106. else
  107. ++j;
  108. }
  109. }
  110. return !list.empty();
  111. }
  112. // Returns true if this range is wholly contained within another range.
  113. bool UnicodeRange::IsContained(const UnicodeRange& rhs)
  114. {
  115. return rhs.min_codepoint <= min_codepoint &&
  116. rhs.max_codepoint >= max_codepoint;
  117. }
  118. // Returns true if this range is wholly contained within another range list.
  119. bool UnicodeRange::IsContained(const UnicodeRangeList& rhs)
  120. {
  121. for (size_t i = 0; i < rhs.size(); ++i)
  122. {
  123. if (IsContained(rhs[i]))
  124. return true;
  125. }
  126. return false;
  127. }
  128. // Returns true if this range is contained or contiguous with another range.
  129. bool UnicodeRange::IsContiguous(const UnicodeRange& rhs)
  130. {
  131. return (min_codepoint >= rhs.min_codepoint && min_codepoint <= ((rhs.max_codepoint == 0xFFFFFFFF) ? rhs.max_codepoint : rhs.max_codepoint + 1)) ||
  132. (max_codepoint >= ((rhs.min_codepoint == 0) ? 0 : rhs.min_codepoint - 1) && max_codepoint <= rhs.max_codepoint);
  133. }
  134. // Joins this range with another that it is contiguous with.
  135. UnicodeRange UnicodeRange::Join(const UnicodeRange& rhs)
  136. {
  137. return UnicodeRange(Math::Min(min_codepoint, rhs.min_codepoint),
  138. Math::Max(max_codepoint, rhs.max_codepoint));
  139. }
  140. }
  141. }