Skip to content

Commit

Permalink
Merge pull request #316 from krymtkts:feature/misc
Browse files Browse the repository at this point in the history
Miscellaneous small optimizations.
  • Loading branch information
krymtkts authored Jan 31, 2025
2 parents 2764077 + 0887757 commit f9c9a52
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
7 changes: 5 additions & 2 deletions src/pocof.Test/Data.fs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ let randomCase (s: string) =
let duNames<'U> = FSharpType.GetUnionCases(typeof<'U>) |> Seq.map _.Name

let randomCases (name: string) =
[ name; String.lower name; String.upper name; randomCase name ]
[ name; name.ToLower(); name.ToUpper(); randomCase name ]

let toIgnoreCaseSet (x: string seq) =
HashSet(x, StringComparer.InvariantCultureIgnoreCase)
Expand Down Expand Up @@ -235,7 +235,10 @@ module ``QueryState toString`` =
| Matcher.Like -> $"""{if data.Invert then "not" else ""}like"""
| Matcher.Match -> $"""{if data.Invert then "not" else ""}match"""

data |> string |> shouldEqual $"{c}{m} {data.Operator}" |> Prop.collect data
data
|> QueryCondition.toString
|> shouldEqual $"{c}{m} {data.Operator}"
|> Prop.collect data

module initConfig =
[<Fact>]
Expand Down
39 changes: 21 additions & 18 deletions src/pocof/Data.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ module LanguageExtension =
let split (separator: string) (s: string) = Regex.Split(s, separator)

module String =
let lower (s: string) = s.ToLower()
let upper (s: string) = s.ToUpper()
let startsWith (value: string) (s: string) = s.StartsWith(value)

let startsWithIgnoreCase (value: string) (s: string) =
Expand All @@ -65,7 +63,6 @@ module LanguageExtension =
s.Split(separators, StringSplitOptions.None)

let equals (opt: StringComparison) (value: string) (s: string) = s.Equals(value, opt)
let trim (s: string) = s.Trim()
let replace (oldValue: string) (newValue: string) (s: string) = s.Replace(oldValue, newValue)
let fromIndex (index: int) (s: string) = s.Substring(index)
let upToIndex (index: int) (s: string) = s.Substring(0, index)
Expand Down Expand Up @@ -146,22 +143,24 @@ module Data =
| Obj(o) -> o[prop]
| Dict(d) -> d[prop]

let unwrap (entries: Entry seq) =
let unwrap (entries: Entry seq) : obj seq =
entries
|> Seq.map (function
| Entry.Dict(dct) -> dct :> obj
| Entry.Dict(dct) -> dct
| Entry.Obj(o) -> o)

let (|Found|_|) aType excludes name =
FSharpType.GetUnionCases aType
|> Array.filter (fun u -> Set.contains u.Name excludes |> not)
|> Array.tryFind (fun u -> u.Name |> String.lower = name)
|> (fun arr ->
match Set.isEmpty excludes with
| true -> arr
| _ -> arr |> Array.filter (fun u -> Set.contains u.Name excludes |> not))
|> Array.tryFind _.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)

let private tryFromStringExcludes<'a> (excludes: Set<string>) s =
let name = String.lower s
let aType = typeof<'a>

match name with
match s with
| Found aType excludes u -> Ok <| (FSharpValue.MakeUnion(u, [||]) :?> 'a)
| _ -> Error <| $"Unknown %s{aType.Name} '%s{s}'."

Expand Down Expand Up @@ -225,31 +224,34 @@ module Data =

[<RequireQualifiedAccess>]
[<NoComparison>]
[<Struct>]
type Matcher =
| Eq
| Like
| Match

override __.ToString() = toString __ |> String.lower
override __.ToString() = toString __ |> _.ToLower()

[<RequireQualifiedAccess>]
module Matcher =
let fromString = fromString<Matcher>

[<RequireQualifiedAccess>]
[<NoComparison>]
[<Struct>]
type Operator =
| And
| Or

override __.ToString() = toString __ |> String.lower
override __.ToString() = toString __ |> _.ToLower()

[<RequireQualifiedAccess>]
module Operator =
let fromString = fromString<Operator>

[<RequireQualifiedAccess>]
[<NoComparison>]
[<Struct>]
type Layout =
| TopDown
| TopDownHalf
Expand All @@ -269,6 +271,7 @@ module Data =

[<RequireQualifiedAccess>]
[<NoComparison>]
[<Struct>]
type Refresh =
| Required
| NotRequired
Expand All @@ -289,6 +292,7 @@ module Data =

[<RequireQualifiedAccess>]
[<NoComparison>]
[<Struct>]
type InputMode =
| Input
// Note: positive number is the backward selection from the cursor. negative number is the forward selection from the cursor.
Expand Down Expand Up @@ -409,21 +413,20 @@ module Data =
CaseSensitive: bool
Invert: bool }

override __.ToString() =
[ match __.CaseSensitive with
module QueryCondition =
let toString (condition: QueryCondition) =
[ match condition.CaseSensitive with
| true -> "c"
| _ -> ""
// NOTE: use ToString to avoid extra branches when calculating coverages.
match __.Matcher, __.Invert with
match condition.Matcher, condition.Invert with
| Matcher.Eq, true -> "ne"
| m, true -> "not" + m.ToString()
| m, _ -> m.ToString()
" "
__.Operator.ToString() ]
condition.Operator.ToString() ]
|> String.concat ""

[<NoComparison>]
module QueryCondition =
let rotateMatcher (condition: QueryCondition) =
{ condition with
Matcher =
Expand Down Expand Up @@ -457,7 +460,7 @@ module Data =

module InternalState =
let queryInfo (state: InternalState) (count: int) =
$" %O{state.QueryCondition} [%d{count}]"
$" %s{state.QueryCondition |> QueryCondition.toString} [%d{count}]"

let getX promptLength (state: InternalState) =
promptLength + state.QueryState.Cursor - state.QueryState.WindowBeginningCursor
Expand Down
2 changes: 1 addition & 1 deletion src/pocof/Query.fs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ module Query =
| _ ->
let is = prepareTest condition

query |> String.trim |> Regex.split @"\s+" |> List.ofSeq |> parseQuery is []
query |> _.Trim() |> Regex.split @"\s+" |> List.ofSeq |> parseQuery is []

let private prepareNotification (query: string) (condition: QueryCondition) =
match condition.Matcher with
Expand Down

0 comments on commit f9c9a52

Please sign in to comment.