Skip to content
This repository has been archived by the owner on Aug 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #77 from hannesm/astring
Browse files Browse the repository at this point in the history
use Astring instead of String and custom helpers
  • Loading branch information
yomimono committed Nov 7, 2016
2 parents 61675a8 + 65c6294 commit 0a639a7
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .merlin
@@ -1,7 +1,7 @@
B _build/*
S src/*
S runtime/*
PKG unix dynlink cmdliner rresult fmt ocamlgraph
PKG unix dynlink cmdliner rresult fmt ocamlgraph astring

FLG -w +A-4-6-7-9-40-42-44-48
FLG -w -32-34-37
Expand Down
8 changes: 3 additions & 5 deletions _tags
@@ -1,5 +1,5 @@
true : bin_annot, safe_string, package(bytes), strict_sequence, short_paths
true: warn(A-40-42-44), warn_error(+1..49), warn_error(-45-3)
true: warn(A-40-42-44), warn_error(+1..49), warn_error(-45)

# Better locations and loc in .mlis
true: keep_locs
Expand All @@ -8,12 +8,10 @@ true: keep_locs
<lib> : include
<runtime>: include

<{lib,app}/*>: \
package(unix), package(dynlink), package(cmdliner), package(rresult), \
package(fmt.tty), package(ocamlgraph)
<{lib,app}/*>: package(unix dynlink cmdliner rresult fmt.tty ocamlgraph astring)

# Force the runtime to be unix-independent.
<runtime/*>: dontlink(unix)
<runtime/*>: package(cmdliner), package(fmt)

<tests/*>: package(oUnit), package(cmdliner), package(fmt.cli), package(rresult)
<tests/*>: package(oUnit cmdliner fmt.cli rresult astring)
16 changes: 10 additions & 6 deletions app/functoria_app.ml
Expand Up @@ -16,6 +16,7 @@
*)

open Rresult
open Astring

open Functoria
include Functoria_misc
Expand Down Expand Up @@ -50,7 +51,7 @@ let sys_argv = impl @@ object
module Keys = struct

let configure i =
let file = String.lowercase Key.module_name ^ ".ml" in
let file = String.Ascii.lowercase Key.module_name ^ ".ml" in
Log.info "%a %s" Log.blue "Generating:" file;
Cmd.with_file (Info.root i / file) @@ fun fmt ->
Codegen.append fmt "(* %s *)" (Codegen.generated_header ());
Expand All @@ -67,7 +68,7 @@ module Keys = struct
R.ok ()

let clean i =
let file = String.lowercase Key.module_name ^ ".ml" in
let file = String.Ascii.lowercase Key.module_name ^ ".ml" in
R.ok @@ Cmd.remove (Info.root i / file)

let name = "key"
Expand Down Expand Up @@ -120,7 +121,7 @@ let app_info ?(type_modname="Functoria_info") ?(gen_modname="Info_gen") () =
inherit base_configurable
method ty = info
method name = "info"
val gen_file = String.lowercase gen_modname ^ ".ml"
val gen_file = String.Ascii.lowercase gen_modname ^ ".ml"
method module_name = gen_modname
method !libraries = Key.pure ["functoria.runtime"]
method !packages = Key.pure ["functoria"]
Expand Down Expand Up @@ -196,7 +197,10 @@ module Engine = struct
let base = c#module_name in
if args = [] then base
else
let prefix = try String.(sub base 0 @@ index base '.') with _ -> base in
let prefix = match String.cut ~sep:"." base with
| Some (l, _) -> l
| None -> base
in
let prefix = Name.ocamlify prefix in
Name.create (Fmt.strf "%s%i" prefix id) ~prefix

Expand Down Expand Up @@ -428,7 +432,7 @@ module Make (P: S) = struct
You can pass the `--no-opam-version-check` flag to force its \
use." opam_version
in
match String.split opam_version '.' with
match String.cuts ~sep:"." opam_version with
| major::minor::_ ->
let major = try int_of_string major with Failure _ -> 0 in
let minor = try int_of_string minor with Failure _ -> 0 in
Expand Down Expand Up @@ -511,7 +515,7 @@ module Make (P: S) = struct
"cd %s && ocamlbuild -use-ocamlfind -tags annot,bin_annot -pkg %s %s"
root P.name file
>>= fun () ->
try Ok (Dynlink.loadfile (String.concat "/" [root; "_build"; file]))
try Ok (Dynlink.loadfile (String.concat ~sep:"/" [root; "_build"; file]))
with Dynlink.Error err ->
Log.error "Error loading config: %s" (Dynlink.error_message err)

Expand Down
4 changes: 3 additions & 1 deletion lib/functoria.ml
Expand Up @@ -15,6 +15,8 @@
*)

open Rresult
open Astring

open Functoria_misc

module Key = Functoria_key
Expand Down Expand Up @@ -107,7 +109,7 @@ class base_configurable = object
method packages: string list Key.value = Key.pure []
method keys: Key.t list = []
method connect (_:Info.t) (_:string) l =
Printf.sprintf "return (%s)" (String.concat ", " l)
Printf.sprintf "return (%s)" (String.concat ~sep:", " l)
method configure (_: Info.t): (unit,string) R.t = R.ok ()
method clean (_: Info.t): (unit,string) R.t = R.ok ()
method deps: abstract_impl list = []
Expand Down
59 changes: 9 additions & 50 deletions lib/functoria_misc.ml
Expand Up @@ -16,6 +16,7 @@
*)

open Rresult
open Astring

let (/) = Filename.concat

Expand All @@ -29,48 +30,6 @@ module type Monoid = sig
val union: t -> t -> t
end

(* {String manipulation} *)

module String = struct

include String

let strip str =
let p = ref 0 in
let l = String.length str in
let fn = function
| ' ' | '\t' | '\r' | '\n' -> true
| _ -> false in
while !p < l && fn (String.unsafe_get str !p) do
incr p;
done;
let p = !p in
let l = ref (l - 1) in
while !l >= p && fn (String.unsafe_get str !l) do
decr l;
done;
String.sub str p (!l - p + 1)

let cut_at s sep =
try
let i = String.index s sep in
let name = String.sub s 0 i in
let version = String.sub s (i+1) (String.length s - i - 1) in
Some (name, version)
with _ ->
None

let split s sep =
let rec aux acc r =
match cut_at r sep with
| None -> List.rev (r :: acc)
| Some (h,t) -> aux (strip h :: acc) t in
aux [] s

module Set = Set.Make (String)

end

(* {Logging} *)

module Log = struct
Expand Down Expand Up @@ -205,7 +164,7 @@ module Cmd = struct
| Some `None -> " --color=never"
| Some `Ansi_tty -> " --color=always"
in
let deps = String.concat " " deps in
let deps = String.concat ~sep:" " deps in
(* Note: we don't redirect output to the log as installation can
* take a long time and the user will want to see what is
happening. *)
Expand Down Expand Up @@ -254,7 +213,7 @@ module Cmd = struct
let collect_output cmd =
try
with_process_in cmd
(fun ic -> Some (String.strip (input_line ic)))
(fun ic -> Some (Astring.String.trim (input_line ic)))
with _ ->
None

Expand Down Expand Up @@ -283,10 +242,10 @@ module Cmd = struct

let ocaml_version () =
let version =
match String.cut_at Sys.ocaml_version '+' with
match Astring.String.cut ~sep:"+" Sys.ocaml_version with
| Some (version, _) -> version
| None -> Sys.ocaml_version in
match String.split version '.' with
match Astring.String.cuts ~sep:"." version with
| major :: minor :: _ ->
begin
try int_of_string major, int_of_string minor
Expand All @@ -299,13 +258,13 @@ module Cmd = struct
let query ?predicates ?(format="%p") ?(recursive=false) xs =
let pred = match predicates with
| None -> ""
| Some ps -> "-predicates '" ^ String.concat "," ps ^ "'"
| Some ps -> "-predicates '" ^ String.concat ~sep:"," ps ^ "'"
and fmt = "-format '" ^ format ^ "'"
and r = if recursive then "-recursive" else ""
and pkgs = String.concat " " xs
and pkgs = String.concat ~sep:" " xs
in
read "ocamlfind query %s %s %s %s" fmt pred r pkgs
>>| fun out -> String.split out '\n'
>>| fun out -> Astring.String.cuts ~sep:"\n" out

let installed lib =
Sys.command ("ocamlfind query " ^ lib ^ " 2>&1 1>/dev/null") = 0
Expand Down Expand Up @@ -412,7 +371,7 @@ module Terminfo = struct
try (* GNU stty *)
with_process_in "stty" "size"
(fun ic ->
match String.split (input_line ic) ' ' with
match Astring.String.cuts ~sep:" " (input_line ic) with
| [_; v] -> int_of_string v
| _ -> failwith "stty")
with
Expand Down
17 changes: 0 additions & 17 deletions lib/functoria_misc.mli
Expand Up @@ -32,23 +32,6 @@ module type Monoid = sig
val union: t -> t -> t
end

(** {2 String utilities} *)

module String: sig
include (module type of String)

module Set: Set.S with type elt = string

val strip: string -> string
(** Remove heading and trailing spaces. *)

val cut_at: string -> char -> (string * string) option
(** Cut at the first occurrence of a given character. *)

val split: string -> char -> string list
(** Split at each occurence of the given character. *)
end

(** {2 Command-line utilities} *)

module Cmd: sig
Expand Down
2 changes: 2 additions & 0 deletions opam
Expand Up @@ -19,10 +19,12 @@ build-test: [
]
depends: [
"ocamlfind" {build}
"ocamlbuild" {build}
"topkg" {build & >= "0.7.3"}
"base-unix"
"cmdliner" {>= "0.9.8"}
"rresult"
"astring"
"fmt"
"ocamlgraph"
"ounit" {test}
Expand Down
10 changes: 6 additions & 4 deletions pkg/META
@@ -1,27 +1,29 @@
description = "Functoria configuration tool"
version = "%%VERSION%%"
requires = "unix dynlink cmdliner rresult fmt.tty functoria.runtime ocamlgraph"
version = "%%VERSION_NUM%%"
requires = "unix dynlink cmdliner rresult fmt.tty functoria.runtime ocamlgraph astring"
archive(byte) = "functoria.cma"
archive(native) = "functoria.cmxa"
plugin(byte) = "functoria.cma"
plugin(native) = "functoria.cmxs"

package "runtime" (
description = "Functoria runtime"
version = "%%VERSION%%"
version = "%%VERSION_NUM%%"
requires = "cmdliner fmt"
archive(byte) = "functoria-runtime.cma"
archive(native) = "functoria-runtime.cmxa"
plugin(byte) = "functoria-runtime.cma"
plugin(native) = "functoria-runtime.cmxs"
exists_if = "functoria-runtime.cma"
)

package "app" (
description = "Functoria tool helpers"
version = "%%VERSION%%"
version = "%%VERSION_NUM%%"
requires = "functoria"
archive(byte) = "functoria-app.cma"
archive(native) = "functoria-app.cmxa"
plugin(byte) = "functoria-app.cma"
plugin(native) = "functoria-app.cmxs"
exists_if = "functoria-app.cma"
)

0 comments on commit 0a639a7

Please sign in to comment.