Commits

Sébastien Ferré committed 820e318

Customization of the writing of floats, to take into account the precision.

Comments (0)

Files changed (1)

     | Quote -> "'", Sep
     | Ident s -> s, Word
     | Int n -> string_of_int n, Word
-    | Float f -> string_of_float f, Word
+    | Float (f,p) -> string_of_float f, Word
+        let s = string_of_float f in
+        let l = String.length s in
+        let prec = prec_of_sfloat s in
+        if prec >= p
+        then s
+        else
+          let zeros = String.make (p-prec) '0' in
+          try
+            let i_e = String.index s 'e' in
+            String.sub s 0 i_e ^ zeros ^ String.sub s i_e (l-i_e) 
+          with Not_found -> s ^ zeros,
+        Word
     | String s -> "\"" ^ String.escaped s ^ "\"", Word
     | Term s ->
 	let b = ref true in
 
 open Format
 
+let rec power10 : int -> float =
+      function
+      | 0 -> 1.
+      | p -> if p > 0
+             then power10 (p-1) *. 10.
+             else power10 (p+1) /. 10.
+
 let pp_print_token : formatter -> Token.t -> Token.t -> unit =
   fun ff pred -> function
     | BackQuote -> pp_print_string ff "`"
 	| Plus | Minus | Int _ | Float _ | Ident _ | Term _ -> pp_print_string ff " "
 	| _ -> ());
 	pp_print_int ff n
-    | Float f ->
+    | Float (f,p) ->
 	( match pred with
 	| Plus | Minus | Int _ | Float _ | Ident _ | Term _ -> pp_print_string ff " "
 	| _ -> ());
-	pp_print_float ff f
+        let sm = if f=0. then "" else string_of_int (int_of_float ((abs_float f) *. (power10 (-p)))) in
+	let l = String.length sm in
+        let e = let x = (p+l) mod 3 in if x >= 0 then x else x+3 in
+        let exp e = if e = 0 then "" else "e" ^ string_of_int e in
+	let s =
+          (if f < 0. then "-" else "") ^
+          if e = 1 then String.sub sm 0 1 ^ "." ^ String.sub sm 1 (l-1) ^ exp (p+l-1)
+          else if e = 2 then
+            if l >= 2 & p+l+1 <> 0 then String.sub sm 0 2 ^ "." ^ String.sub sm 1 (l-2) ^ exp (p+l-2)
+            else "0.0" ^ sm ^ exp (p + l + 1)
+          else
+            if l >= 3 & p+l <> 0 then String.sub sm 0 3 ^ "." ^ String.sub sm 1 (l-3) ^ exp (p+l-3)
+            else "0." ^ sm ^ exp (p + l) in
+	pp_print_string ff s
     | String s -> pp_print_string ff ("\"" ^ String.escaped s ^ "\"")
     | Term s ->
 	( match pred with
   | [<'Token.Ident s when s = spec.n; q = parse_fact spec>] -> spec.neg q
   | [<a = spec.atom>] -> a
 
+(* generic functions about strings *)
+(* ------------------------------- *)
+
+let rec split (normalize, separator, stopword) (s : string) = 
+      split2 (normalize, separator, stopword) s 0 "" []
+and split2 (normalize, separator, stopword) s i w ws =
+      if i>=String.length s then addword (normalize, separator, stopword) w ws
+      else if separator s.[i] then split2 (normalize, separator, stopword) s (i+1) "" (addword (normalize, separator, stopword) w ws)
+      else split2 (normalize, separator, stopword) s (i+1) (w ^ String.make 1 s.[i]) ws
+and addword (normalize, separator, stopword) w ws =
+      if w = "" or stopword (normalize w) then ws else w::ws
 
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.