diff options
author | Pierre Weis <Pierre.Weis@inria.fr> | 2003-04-28 08:13:20 +0000 |
---|---|---|
committer | Pierre Weis <Pierre.Weis@inria.fr> | 2003-04-28 08:13:20 +0000 |
commit | e9cda5216baa14984ea52887d1d45a16f3bcecc7 (patch) | |
tree | d3ee40faaf2344fea3e7fc6fd005e481b4f41eab | |
parent | 55edf60997a566cc967a592beee26e2241b9afcf (diff) | |
download | ocaml-e9cda5216baa14984ea52887d1d45a16f3bcecc7.tar.gz |
Addition of function add_substitute for adding strings to buffer with
variable names substitution via a function mapping.
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@5516 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
-rw-r--r-- | stdlib/buffer.ml | 68 | ||||
-rw-r--r-- | stdlib/buffer.mli | 16 |
2 files changed, 82 insertions, 2 deletions
diff --git a/stdlib/buffer.ml b/stdlib/buffer.ml index e31b7b80aa..ce9b5c1f1c 100644 --- a/stdlib/buffer.ml +++ b/stdlib/buffer.ml @@ -80,3 +80,71 @@ let add_channel b ic len = let output_buffer oc b = output oc b.buffer 0 b.position + +let closing = function + | '(' -> ')' + | '{' -> '}' + | _ -> assert false;; + +(* opening and closing: open and close characters, typically ( and ) + k balance of opening and closing chars + s the string where we are searching + p the index where we start the search *) +let advance_to_closing opening closing k s start = + let rec advance k i lim = + if i >= lim then raise Not_found else + if s.[i] = opening then advance (k + 1) (i + 1) lim else + if s.[i] = closing then + if k = 0 then i else advance (k - 1) (i + 1) lim + else advance k (i + 1) lim in + advance k start (String.length s);; + +let advance_to_non_alpha s start = + let rec advance i lim = + if i >= lim then lim else + match s.[i] with + | 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '_' | + 'é'|'à'|'á'|'è'|'ù'|'â'|'ê'|'î'|'ô'|'û'|'ë'|'ï'|'ü'|'ç'| + 'É'|'À'|'Á'|'È'|'Ù'|'Â'|'Ê'|'Î'|'Ô'|'Û'|'Ë'|'Ï'|'Ü'|'Ç' -> + advance (i + 1) lim + | _ -> i in + advance start (String.length s);; + +(* We are just at the beginning of an ident in s, starting at p *) +let find_ident s start = + match s.[start] with + (* Parenthesized ident ? *) + | '(' | '{' as c -> + let new_start = start + 1 in + let stop = advance_to_closing c (closing c) 0 s new_start in + String.sub s new_start (stop - start - 1), stop + 1 + (* Regular ident *) + | _ -> + let stop = advance_to_non_alpha s (start + 1) in + String.sub s start (stop - start), stop;; + +(* Substitute $ident (or $(ident)) in s, + according to the function f. *) +let add_substitute b f s = + let lim = String.length s in + let rec subst previous i = + if i < lim then begin + match s.[i] with + | '$' as current when previous = '\\' -> + add_char b current; + subst current (i + 1) + | '$' -> + let ident, next_i = find_ident s (i + 1) in + add_string b (f ident); + subst ' ' next_i + | current when previous == '\\' -> + add_char b '\\'; + add_char b current; + subst current (i + 1) + | '\\' as current -> + subst current (i + 1) + | current -> + add_char b current; + subst current (i + 1) + end in + subst ' ' 0;; diff --git a/stdlib/buffer.mli b/stdlib/buffer.mli index 849f489ffe..02451c51dd 100644 --- a/stdlib/buffer.mli +++ b/stdlib/buffer.mli @@ -25,14 +25,14 @@ type t val create : int -> t (** [create n] returns a fresh buffer, initially empty. The [n] parameter is the initial size of the internal string - that holds the buffer contents. That string is automatically + that holds the buffer contents. That string is automatically reallocated when more than [n] characters are stored in the buffer, but shrinks back to [n] characters when [reset] is called. For best performance, [n] should be of the same order of magnitude as the number of characters that are expected to be stored in the buffer (for instance, 80 for a buffer that holds one output line). Nothing bad will happen if the buffer grows beyond that - limit, however. In doubt, take [n = 16] for instance. + limit, however. In doubt, take [n = 16] for instance. If [n] is not between 1 and {!Sys.max_string_length}, it will be clipped to that interval. *) @@ -63,6 +63,18 @@ val add_substring : t -> string -> int -> int -> unit (** [add_substring b s ofs len] takes [len] characters from offset [ofs] in string [s] and appends them at the end of the buffer [b]. *) +val add_substitute : t -> (string -> string) -> string -> unit +(** [add_substitute b f s] appends the string [s] at the end of the buffer [b] + with substitution: variable names in [s] get replaced by their image by [f]. + A variable name is defined as a non empty sequence of alphanumeric or [_] + characters (or alternatively an arbitrary sequence of characters + enclosed by a pair of matching parentheses or curly brackets), + that immediately follows a (non-escaped) [$] character; + an escaped [$] character is a [$] that immediately follows + a backslash character; it then stands for a plain [$]. + Raise [Not_found] if the closing character of a parenthesized variable + cannot be found. *) + val add_buffer : t -> t -> unit (** [add_buffer b1 b2] appends the current contents of buffer [b2] at the end of buffer [b1]. [b2] is not modified. *) |