Imported Upstream version 8.2.2
[platform/upstream/harfbuzz.git] / src / hb-utf.hh
index ff5712d..1120bd1 100644 (file)
@@ -35,6 +35,7 @@
 struct hb_utf8_t
 {
   typedef uint8_t codepoint_t;
+  static constexpr unsigned max_len = 4;
 
   static const codepoint_t *
   next (const codepoint_t *text,
@@ -182,6 +183,7 @@ struct hb_utf16_xe_t
 {
   static_assert (sizeof (TCodepoint) == 2, "");
   typedef TCodepoint codepoint_t;
+  static constexpr unsigned max_len = 2;
 
   static const codepoint_t *
   next (const codepoint_t *text,
@@ -290,6 +292,7 @@ struct hb_utf32_xe_t
 {
   static_assert (sizeof (TCodepoint) == 4, "");
   typedef TCodepoint codepoint_t;
+  static constexpr unsigned max_len = 1;
 
   static const TCodepoint *
   next (const TCodepoint *text,
@@ -348,6 +351,7 @@ typedef hb_utf32_xe_t<uint32_t, false> hb_utf32_novalidate_t;
 struct hb_latin1_t
 {
   typedef uint8_t codepoint_t;
+  static constexpr unsigned max_len = 1;
 
   static const codepoint_t *
   next (const codepoint_t *text,
@@ -399,12 +403,13 @@ struct hb_latin1_t
 struct hb_ascii_t
 {
   typedef uint8_t codepoint_t;
+  static constexpr unsigned max_len = 1;
 
   static const codepoint_t *
   next (const codepoint_t *text,
        const codepoint_t *end HB_UNUSED,
        hb_codepoint_t *unicode,
-       hb_codepoint_t replacement HB_UNUSED)
+       hb_codepoint_t replacement)
   {
     *unicode = *text++;
     if (*unicode >= 0x0080u)
@@ -450,4 +455,27 @@ struct hb_ascii_t
   }
 };
 
+template <typename utf_t>
+static inline const typename utf_t::codepoint_t *
+hb_utf_offset_to_pointer (const typename utf_t::codepoint_t *start,
+                         signed offset)
+{
+  hb_codepoint_t unicode;
+
+  while (offset-- > 0)
+    start = utf_t::next (start,
+                        start + utf_t::max_len,
+                        &unicode,
+                        HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT);
+
+  while (offset++ < 0)
+    start = utf_t::prev (start,
+                        start - utf_t::max_len,
+                        &unicode,
+                        HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT);
+
+  return start;
+}
+
+
 #endif /* HB_UTF_HH */