[libc-commits] [libc] [libc][regex] Add bracket expressions and character ranges (PR #234895)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Fri Oct 23 04:53:15 PDT 2026


================
@@ -64,6 +64,91 @@ constexpr bool is_unimplemented_ere_escape(char c) {
   }
 }
 
+// Parses a single collating element [[.c.]] or equivalence class [[=c=]].
+ErrorOr<uint8_t> parse_coll_elem(cpp::string_view &rem) {
+  cpp::string_view close_delim = rem.starts_with("[.") ? ".]" : "=]";
+  rem.remove_prefix(2);
+  size_t delim_pos = 0;
+  while (delim_pos + 2 <= rem.size() &&
+         !rem.substr(delim_pos).starts_with(close_delim))
+    ++delim_pos;
+  if (delim_pos + 2 > rem.size())
+    return Error(REG_EBRACK);
+  cpp::string_view elem = rem.substr(0, delim_pos);
+  rem.remove_prefix(delim_pos + 2);
+  if (elem.size() == 1)
+    return static_cast<uint8_t>(elem.front());
+  if (elem == "one")
+    return static_cast<uint8_t>('1');
+  return Error(REG_ECOLLATE);
+}
+
+// Parses a single bracket expression item (collating element or single byte).
+ErrorOr<uint8_t> parse_bracket_elem(cpp::string_view &rem) {
+  if (rem.starts_with("[:"))
+    return Error(REG_BADPAT);
+  if (rem.starts_with("[.") || rem.starts_with("[="))
+    return parse_coll_elem(rem);
+  uint8_t c = static_cast<uint8_t>(rem.front());
+  rem.remove_prefix(1);
+  return c;
+}
+
+// Parses a POSIX bracket expression [...], returning a ByteSet node.
+ErrorOr<ExprId> parse_bracket_expr(cpp::string_view &rem, int cflags,
+                                   ExprPool &pool) {
+  if (rem.empty())
+    return Error(REG_EBRACK);
----------------
labath wrote:

I think this check, and the one on line 107 is superfluous. The code will return REG_EBRACK anyway.

https://github.com/llvm/llvm-project/pull/234895


More information about the libc-commits mailing list