From e2b0d8130c73fc8c0469dd35f654799146409c4c Mon Sep 17 00:00:00 2001 From: Niols Date: Sun, 18 Jun 2023 21:14:31 +0200 Subject: [PATCH] Move `merge_leading_literals` to `CSTHelpers` --- src/CSTHelpers.ml | 14 ++++++++++++++ src/CSTHelpers.mli | 3 +++ src/tildePrefix.ml | 17 +---------------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/CSTHelpers.ml b/src/CSTHelpers.ml index 2f08e350..b29529cc 100644 --- a/src/CSTHelpers.ml +++ b/src/CSTHelpers.ml @@ -230,3 +230,17 @@ let io_redirect_list_of_simple_command = function io_redirect_list_of_cmd_suffix cmd_suffix'.value | SimpleCommand_CmdName _ -> [] + +let merge_leading_literals = + let buf = Buffer.create 80 in + let rec extract_leading_literals = function + | WordLiteral lit :: rest -> + Buffer.add_string buf lit; + extract_leading_literals rest + | rest -> rest + in + fun word -> + let rest = extract_leading_literals word in + let lit = Buffer.contents buf in + Buffer.reset buf; + if lit = "" then word else WordLiteral lit :: rest diff --git a/src/CSTHelpers.mli b/src/CSTHelpers.mli index 28e0fa2c..e5d8388a 100644 --- a/src/CSTHelpers.mli +++ b/src/CSTHelpers.mli @@ -71,3 +71,6 @@ val io_redirect_list_of_cmd_prefix : cmd_prefix -> io_redirect' list val io_redirect_list_of_cmd_suffix : cmd_suffix -> io_redirect' list val io_redirect_list_of_simple_command : simple_command -> io_redirect' list val io_redirect_list_of_redirect_list : redirect_list -> io_redirect' list + +(** Merges several leading [WordLiteral] into one. *) +val merge_leading_literals : word_cst -> word_cst diff --git a/src/tildePrefix.ml b/src/tildePrefix.ml index 7d75b656..aa0c4df9 100644 --- a/src/tildePrefix.ml +++ b/src/tildePrefix.ml @@ -61,25 +61,10 @@ let extract_tilde_prefix_from_literal (literal : string) : word_cst = let (first, rest) = ExtPervasives.string_split i literal in [WordTildePrefix (strip_tilde first); WordLiteral rest] -(** Merges several leading [WordLiteral] into one. *) -let merge_leading_literals : word_cst -> word_cst = - let buf = Buffer.create 80 in - let rec extract_leading_literals = function - | WordLiteral lit :: rest -> - Buffer.add_string buf lit; - extract_leading_literals rest - | rest -> rest - in - fun word -> - let rest = extract_leading_literals word in - let lit = Buffer.contents buf in - Buffer.reset buf; - if lit = "" then word else WordLiteral lit :: rest - (** Extracts the tilde-prefix at the beginning of the given word CST if there is one. Otherwise, returns the word as-is. *) let extract_tilde_prefix_from_word_if_present (word : word_cst) : word_cst = - match merge_leading_literals word with + match CSTHelpers.merge_leading_literals word with | WordLiteral literal :: word when starts_with_tilde literal -> extract_tilde_prefix_from_literal literal @ word | word -> word