From 84e358b971d73ad7c12e4b796ac15bdfa6c226b8 Mon Sep 17 00:00:00 2001 From: Cuihtlauac ALVARADO Date: Wed, 21 Oct 2026 08:57:48 +0200 Subject: [PATCH] graphql_parser: drop the `re` dependency `re` was pulled into `graphql_parser` only for a handful of single-character/newline operations, forcing a full regex engine onto every downstream package. - `block_string.ml`: replace `Re.split` (split on \r\n, \n, \r) with a hand-rolled `split_lines` that scans the string directly, mirroring the existing `get_block_string_indentation` logic. - `ast.ml`: replace the chain of `Re.replace_string` calls in `quote_string` with a single-pass buffer escape. - Drop `re` from `graphql_parser/src/dune` and `graphql_parser.opam`. Closes #221 Co-Authored-By: Claude Opus 4.8 (1M context) --- CHANGES.md | 5 +++++ graphql_parser.opam | 1 - graphql_parser/src/ast.ml | 23 ++++++++++++++--------- graphql_parser/src/block_string.ml | 26 ++++++++++++++++++-------- graphql_parser/src/dune | 2 +- 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index b14c576..4b2127a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,8 @@ +Unreleased +--------------------------------- + +- Drop the `re` dependency from `graphql_parser` (#221) + 0.14.0 2022-07-08 --------------------------------- diff --git a/graphql_parser.opam b/graphql_parser.opam index 872916c..26b6501 100644 --- a/graphql_parser.opam +++ b/graphql_parser.opam @@ -17,7 +17,6 @@ depends: [ "menhir" {build} "alcotest" {with-test & >= "0.8.1"} "fmt" - "re" {>= "1.5.0"} ] synopsis: "Library for parsing GraphQL queries" diff --git a/graphql_parser/src/ast.ml b/graphql_parser/src/ast.ml index ae41a9c..e593ef2 100644 --- a/graphql_parser/src/ast.ml +++ b/graphql_parser/src/ast.ml @@ -77,15 +77,20 @@ module Pp = struct let colon : unit Fmt.t = Fmt.(const string ":") let quote_string s = - let open Re in - s - |> replace_string (compile (char '\\')) ~by:"\\\\" - |> replace_string (compile (char '"')) ~by:"\\\"" - |> replace_string (compile (char '\b')) ~by:"\\b" - |> replace_string (compile (char '\012')) ~by:"\\f" - |> replace_string (compile (char '\n')) ~by:"\\n" - |> replace_string (compile (char '\r')) ~by:"\\r" - |> replace_string (compile (char '\t')) ~by:"\\t" + let b = Buffer.create (String.length s) in + String.iter + (fun c -> + match c with + | '\\' -> Buffer.add_string b "\\\\" + | '"' -> Buffer.add_string b "\\\"" + | '\b' -> Buffer.add_string b "\\b" + | '\012' -> Buffer.add_string b "\\f" + | '\n' -> Buffer.add_string b "\\n" + | '\r' -> Buffer.add_string b "\\r" + | '\t' -> Buffer.add_string b "\\t" + | c -> Buffer.add_char b c) + s; + Buffer.contents b let rec pp_value fmt = function | `Null -> Fmt.string fmt "null" diff --git a/graphql_parser/src/block_string.ml b/graphql_parser/src/block_string.ml index 797cd7b..e1fdddb 100644 --- a/graphql_parser/src/block_string.ml +++ b/graphql_parser/src/block_string.ml @@ -24,6 +24,23 @@ let get_block_string_indentation (s : string) : int = done; match !common_indent with None -> 0 | Some num -> num +let split_lines (s : string) : string list = + let lines = ref [] in + let b = Buffer.create 32 in + let i = ref 0 in + let n = String.length s in + while !i < n do + ( match s.[!i] with + | ('\r' as c) | ('\n' as c) -> + if c == '\r' && try s.[!i + 1] == '\n' with _ -> false then incr i; + lines := Buffer.contents b :: !lines; + Buffer.clear b + | c -> Buffer.add_char b c ); + incr i + done; + lines := Buffer.contents b :: !lines; + List.rev !lines + exception NotBlank let is_blank (s : string) : bool = @@ -33,14 +50,7 @@ let is_blank (s : string) : bool = with NotBlank -> false let dedent_block_string_value (s : string) : string = - let lines = - (let open Re in - split - (compile - (alt [ seq [ char '\r'; char '\n' ]; alt [ char '\n'; char '\r' ] ])) - s) - |> Array.of_list - in + let lines = split_lines s |> Array.of_list in let start_line = ref 0 in while !start_line < Array.length lines && is_blank lines.(!start_line) do incr start_line diff --git a/graphql_parser/src/dune b/graphql_parser/src/dune index 478fd7f..f29f07a 100644 --- a/graphql_parser/src/dune +++ b/graphql_parser/src/dune @@ -7,6 +7,6 @@ (library (name graphql_parser) (public_name graphql_parser) - (libraries fmt re) + (libraries fmt) (flags (:standard -w -30)))