diff --git a/src/pocof.Test/PocofData.fs b/src/pocof.Test/PocofData.fs index 1121e395..ea10b979 100644 --- a/src/pocof.Test/PocofData.fs +++ b/src/pocof.Test/PocofData.fs @@ -27,9 +27,7 @@ module ``Action fromString should returns`` = String.lower a.Name String.upper a.Name ] |> List.map Action.fromString - |> List.iter (fun s -> - s - |> shouldEqual (Ok(FSharpValue.MakeUnion(a, [||]) :?> Action)))) + |> List.iter (shouldEqual (Ok(FSharpValue.MakeUnion(a, [||]) :?> Action)))) let ``Error Unknown.``<'a> (fromString: string -> 'a) = shouldFail (fun () -> fromString "Unknown" |> ignore) @@ -41,9 +39,7 @@ let ``known matchers.``<'a> (fromString: string -> 'a) = String.lower a.Name String.upper a.Name ] |> List.map fromString - |> List.iter (fun s -> - s - |> shouldEqual (FSharpValue.MakeUnion(a, [||]) :?> 'a))) + |> List.iter (shouldEqual (FSharpValue.MakeUnion(a, [||]) :?> 'a))) module ``Matcher fromString should returns`` = [] @@ -229,7 +225,7 @@ module invokeAction = Operator = OR CaseSensitive = false Invert = false } - PropertySearch = NonSearch + PropertySearch = NoSearch Notification = "" SuppressProperties = false } @@ -259,7 +255,7 @@ module invokeAction = |> shouldEqual ( { state with Query = ":name " - PropertySearch = NonSearch }, + PropertySearch = NoSearch }, { X = 6; Y = 0 }, Required ) @@ -270,7 +266,7 @@ module invokeAction = let state = { state with Query = ":name" - PropertySearch = NonSearch } + PropertySearch = NoSearch } let position: Position = { X = 0; Y = 0 } @@ -344,7 +340,7 @@ module invokeAction = |> shouldEqual ( { state with Query = ":name" - PropertySearch = NonSearch }, + PropertySearch = NoSearch }, { X = 0; Y = 0 }, Required ) @@ -361,7 +357,7 @@ module invokeAction = |> shouldEqual ( { state with Query = ":name" - PropertySearch = NonSearch }, + PropertySearch = NoSearch }, { X = 0; Y = 0 }, NotRequired ) @@ -371,14 +367,14 @@ module invokeAction = invokeAction { state with Query = "query" - PropertySearch = NonSearch } + PropertySearch = NoSearch } { X = 5; Y = 0 } [] BeginningOfLine |> (shouldEqual ( { state with Query = "query" - PropertySearch = NonSearch }, + PropertySearch = NoSearch }, { X = 0; Y = 0 }, NotRequired )) @@ -423,14 +419,14 @@ module invokeAction = invokeAction { state with Query = "query" - PropertySearch = NonSearch } + PropertySearch = NoSearch } { X = 0; Y = 0 } [] EndOfLine |> shouldEqual ( { state with Query = "query" - PropertySearch = NonSearch }, + PropertySearch = NoSearch }, { X = 5; Y = 0 }, NotRequired ) @@ -441,7 +437,7 @@ module invokeAction = invokeAction { state with Query = ":name " - PropertySearch = NonSearch } + PropertySearch = NoSearch } { X = 6; Y = 0 } [] DeleteBackwardChar @@ -458,14 +454,14 @@ module invokeAction = invokeAction { state with Query = ":name" - PropertySearch = NonSearch } + PropertySearch = NoSearch } { X = 0; Y = 0 } [] DeleteBackwardChar |> shouldEqual ( { state with Query = ":name" - PropertySearch = NonSearch }, + PropertySearch = NoSearch }, { X = 0; Y = 0 }, NotRequired ) @@ -483,7 +479,7 @@ module invokeAction = |> shouldEqual ( { state with Query = "name " - PropertySearch = NonSearch }, + PropertySearch = NoSearch }, { X = 0; Y = 0 }, Required ) diff --git a/src/pocof.Test/PocofQuery.fs b/src/pocof.Test/PocofQuery.fs index acf5dff6..80eb1504 100644 --- a/src/pocof.Test/PocofQuery.fs +++ b/src/pocof.Test/PocofQuery.fs @@ -12,7 +12,7 @@ let initState () : PocofData.InternalState = Operator = PocofData.Operator.OR CaseSensitive = false Invert = false } - PropertySearch = PocofData.PropertySearch.NonSearch + PropertySearch = PocofData.PropertySearch.NoSearch Notification = "" SuppressProperties = false } @@ -29,7 +29,7 @@ module props = [] let ``should returns OK with empty list.`` () = - PocofQuery.props { state with PropertySearch = PocofData.PropertySearch.NonSearch } entries + PocofQuery.props { state with PropertySearch = PocofData.PropertySearch.NoSearch } entries |> shouldEqual (Ok []) [] @@ -224,7 +224,7 @@ module run = let props = Map [] open System.Collections - let mapToDict = List.map (PocofData.Dict) + let mapToDict = List.map PocofData.Dict let entries = mapToDict [ DictionaryEntry("John", "Doe") diff --git a/src/pocof.Test/PocofUI.fs b/src/pocof.Test/PocofUI.fs index d69beefa..7e5ad29c 100644 --- a/src/pocof.Test/PocofUI.fs +++ b/src/pocof.Test/PocofUI.fs @@ -55,7 +55,7 @@ module ``Buff writeScreen`` = Operator = AND CaseSensitive = true Invert = false } - PropertySearch = NonSearch + PropertySearch = NoSearch Notification = "" SuppressProperties = false } @@ -78,7 +78,7 @@ module ``Buff writeScreen`` = Operator = OR CaseSensitive = false Invert = true } - PropertySearch = NonSearch + PropertySearch = NoSearch Notification = "" SuppressProperties = false } diff --git a/src/pocof/Action.fs b/src/pocof/Action.fs index a675fc84..bb5bbf16 100644 --- a/src/pocof/Action.fs +++ b/src/pocof/Action.fs @@ -56,7 +56,7 @@ module PocofAction = | _ -> None let toKeyPattern (s: string) = - match s.Split '+' |> List.ofSeq |> List.rev with + match s |> String.split "+" |> List.ofSeq |> List.rev with | [] -> failwith "Unreachable pass." | [ k ] -> match toEnum k with diff --git a/src/pocof/Data.fs b/src/pocof/Data.fs index ea5317cd..c606020c 100644 --- a/src/pocof/Data.fs +++ b/src/pocof/Data.fs @@ -47,6 +47,11 @@ module LanguageExtension = static member inline lower(s: string) = s.ToLower() static member inline upper(s: string) = s.ToUpper() static member inline startsWith (value: string) (s: string) = s.StartsWith(value) + static member inline split (separator: string) (s: string) = s.Split(separator.ToCharArray()) + static member inline equals (opt: StringComparison) (value: string) (s: string) = s.Equals(value, opt) + + let inline swap (l, r) = (r, l) + let inline alwaysTrue _ = true module PocofData = open System @@ -60,13 +65,12 @@ module PocofData = let unwrap (entries: Entry list) = entries - |> List.map (fun o -> - match o with + |> List.map (function | Dict (dct) -> dct :> obj | Obj (o) -> o) - let private tryFromStringExcludes<'a> (excludes: Set) s = + let inline private tryFromStringExcludes<'a> (excludes: Set) s = let name = String.lower s match FSharpType.GetUnionCases typeof<'a> @@ -76,7 +80,7 @@ module PocofData = | Some u -> Ok <| (FSharpValue.MakeUnion(u, [||]) :?> 'a) | _ -> Error <| sprintf "Unknown case '%s'." s - let private fromString<'a> s = + let inline private fromString<'a> s = let name = String.lower s match FSharpType.GetUnionCases typeof<'a> @@ -85,7 +89,7 @@ module PocofData = | Some u -> FSharpValue.MakeUnion(u, [||]) :?> 'a | _ -> failwithf "Unknown case '%s'." s - let private toString (x: 'a) = + let inline private toString (x: 'a) = match FSharpValue.GetUnionFields(x, typeof<'a>) with | case, _ -> case.Name @@ -141,7 +145,7 @@ module PocofData = static member fromString = fromString type PropertySearch = - | NonSearch + | NoSearch | Search of string | Rotate of string * int * string list @@ -205,11 +209,11 @@ module PocofData = | _ -> None let private getCurrentProperty (query: string) (x: int) = - let s = query.[..x].Split [| ' ' |] |> Seq.last + let s = query.[..x] |> String.split " " |> Seq.last match s with - | Prefix ":" p -> Search <| p - | _ -> NonSearch + | Prefix ":" p -> Search p + | _ -> NoSearch let initConfig (p: IncomingParameters) = // TODO: Eliminate the possibility of failure from here. @@ -255,9 +259,9 @@ module PocofData = { state with PropertySearch = getCurrentProperty state.Query <| p.X - 1 }, p, changed let private moveHead (state: InternalState) (pos: Position) = - { state with PropertySearch = NonSearch }, + { state with PropertySearch = NoSearch }, { pos with X = 0 }, - (pos.X <> 0 && state.PropertySearch <> NonSearch) + (pos.X <> 0 && state.PropertySearch <> NoSearch) |> Refresh.ofBool let private moveTail (state: InternalState) (pos: Position) = @@ -360,7 +364,7 @@ module PocofData = Required match state.PropertySearch with - | NonSearch -> state, pos, NotRequired + | NoSearch -> state, pos, NotRequired | Search keyword -> let candidate, candidates = props diff --git a/src/pocof/Query.fs b/src/pocof/Query.fs index 3fef6367..4973c331 100644 --- a/src/pocof/Query.fs +++ b/src/pocof/Query.fs @@ -7,43 +7,43 @@ open System.Text.RegularExpressions open PocofData module PocofQuery = - let private equalOpt = - function + let inline private equalOpt sensitive = + match sensitive with | true -> StringComparison.CurrentCulture | _ -> StringComparison.CurrentCultureIgnoreCase - let private likeOpt = - function + let inline private likeOpt sensitive = + match sensitive with | true -> WildcardOptions.None | _ -> WildcardOptions.IgnoreCase - let private matchOpt = - function + let inline private matchOpt sensitive = + match sensitive with | true -> RegexOptions.None | _ -> RegexOptions.IgnoreCase - let private (==) opt r l = + let inline private equals (opt: StringComparison) (r: string) = match r with - | "" -> true - | _ -> r.Equals(l, opt) + | "" -> alwaysTrue + | _ -> String.equals opt r - let private (=*=) (opt: WildcardOptions) wcp o = + let inline private likes (opt: WildcardOptions) (wcp: string) = match wcp with - | "" -> true - | _ -> WildcardPattern.Get(wcp, opt).IsMatch o + | "" -> alwaysTrue + | _ -> WildcardPattern.Get(wcp, opt).IsMatch - let private (=~=) opt pattern (o: string) = + let inline private matches (opt: RegexOptions) (pattern: string) (value: string) = try - new Regex(pattern, opt) + // NOTE: expect using cache. + Regex.IsMatch(value, pattern, opt) with - | _ -> new Regex(String.Empty, opt) - |> fun r -> r.IsMatch o + | _ -> true type Query = | Normal of string | Property of string * string - let inline private (/?) (x: 'a) (prop: string) = + let inline private (?=>) (x: 'a) (prop: string) = try // TODO: not so good. let propInfo = x.GetType().GetProperty prop @@ -51,7 +51,7 @@ module PocofQuery = with | _ -> None - let inline private (/?/) (x: PSObject) (prop: string) = + let inline private (?->) (x: PSObject) (prop: string) = try Some (x.Properties.Item prop).Value with @@ -85,7 +85,8 @@ module PocofQuery = let run (state: InternalState) (entries: Entry list) (props: Map) = let queries = - state.Query.Trim().Split [| ' ' |] + state.Query.Trim() + |> String.split " " |> List.ofSeq |> parseQuery [] @@ -106,8 +107,8 @@ module PocofQuery = let p = match o with - | Dict (dct) -> dct /? pk - | Obj (o) -> o /?/ pk + | Dict (dct) -> dct ?=> pk + | Obj (o) -> o ?-> pk match p with | Some (pv) -> (pv, v) :: acc @@ -119,13 +120,17 @@ module PocofQuery = [] |> List.map (fun (s, v) -> (string s, v)) - let is q = + let test = + match state.QueryState.Operator with + | OR -> List.exists + | _ -> List.forall + + let is = match state.QueryState.Matcher with - | EQ -> (==) << equalOpt - | LIKE -> (=*=) << likeOpt - | MATCH -> (=~=) << matchOpt + | EQ -> equals << equalOpt + | LIKE -> likes << likeOpt + | MATCH -> matches << matchOpt <| state.QueryState.CaseSensitive - <| q let answer = match String.IsNullOrWhiteSpace state.Query, state.QueryState.Invert with @@ -133,15 +138,9 @@ module PocofQuery = | _ -> id let predicate (o: Entry) = - let test = - match state.QueryState.Operator with - | OR -> List.exists - | _ -> List.forall - match values o with | [] -> true - | xs -> xs |> test (fun (l, r) -> is r l |> answer) - + | xs -> xs |> test (fun x -> x |> swap ||> is |> answer) let notification = match state.QueryState.Matcher with diff --git a/src/pocof/UI.fs b/src/pocof/UI.fs index 9ec97fb5..060cbeee 100644 --- a/src/pocof/UI.fs +++ b/src/pocof/UI.fs @@ -120,7 +120,8 @@ module PocofScreen = |> Seq.fold (fun acc s -> // NOTE: This split lines is implemented complicated way because of netstandard2.0. - s.Replace(Environment.NewLine, "\n").Split('\n') + s.Replace(Environment.NewLine, "\n") + |> String.split "\n" |> List.ofArray |> (@) acc) []