UnicodeRange.cpp 5.0 KB

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