Commits

juster committed 81306fa

Adds default substition. (ex: ${FOO:-default})

Comments (0)

Files changed (1)

         let rec expand_name str idx params =
 
           let inbrackets = fun str -> function (idx, name) ->
+
+            (* Similiar to pexpand. Expand everything until closing bracket. *)
+            (* XXX: getting tired of passing params around, just use a ref? *)
+            let rec uptoclose str idx =
+              try
+                (* Look ahead for the next } and $. *)
+                let brckidx = String.index_from str idx '}' in
+                begin
+                  try
+                    let sigidx = String.index_from str idx '$' in
+                    if sigidx > brckidx then raise Not_found
+                    else
+                      (* XXX: Copy/pasted from pexpand below. Refactor? *)
+                      let donechunk = String.sub str idx (sigidx - idx) in
+                      match expand_name str (sigidx + 1) params
+                      with (nextidx, expanded) ->
+                        (* Repeat because the bracket could be gone now. *)
+                        match (uptoclose str nextidx) with (newidx, fin) ->
+                          (newidx, donechunk ^ expanded ^ fin)
+                    
+                  with Not_found ->
+                    (* No $ was found or it comes after the closing bracket. *)
+                    (brckidx + 1, String.sub str idx (brckidx - idx))
+                end
+              (* If no } was found we have a problem. *)
+              with Not_found -> raise (ExpandError idx)
+            in
+
+            let inbrackets_subst name str idx =
+              (* XXX: What about badly formed input? *)
+              match uptoclose str (idx + 1) with (newidx, subst) ->
+                let pval = Bashparams.string_of_param (get_param name params) in
+                let expstr =
+                  match str.[idx] with
+                  | '-' -> if "" = pval then subst else pval
+                  |  _  -> ""
+                in (newidx, expstr)
+            in
+
             match str.[idx] with
-              '}' -> (idx+1, name)
-            | _   -> raise Not_found
-          in
+            | '}' -> (idx+1, Bashparams.string_of_param (get_param name params))
+            | ':' -> inbrackets_subst name str (idx+1)
+            |  _  -> raise Not_found
+          in (* end of inbrackets *)
 
           let extractname str idx =
             if Str.string_match (Str.regexp "[a-zA-Z_-]+") str idx then
 let _ =
   let paramslist = [ ("FOO", Bashparams.param_of_string "BAR") ;
                      ("BAR", Bashparams.param_of_string "Hello, World!") ] in
-  print_endline (Bashexpand.parameter_expand "01$" paramslist)
+  print_endline (Bashexpand.parameter_expand
+                   "Substitutions${BAZ:- IS WORKING}?" paramslist)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.