Skip to content

Commit

Permalink
Merge pull request #2963 from dawedawe/refactor_string_comparisons
Browse files Browse the repository at this point in the history
Refactor string comparisons
  • Loading branch information
dawedawe authored Oct 13, 2023
2 parents afa24d6 + 11ac94a commit c7b3918
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 48 deletions.
9 changes: 6 additions & 3 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,11 @@ pipeline "FormatChanged" {
|> Array.choose (fun line ->
let line = line.Trim()
if
(line.StartsWith("AM") || line.StartsWith("M"))
&& (line.EndsWith(".fs") || line.EndsWith(".fsx") || line.EndsWith(".fsi"))
(line.StartsWith("AM", StringComparison.Ordinal)
|| line.StartsWith("M", StringComparison.Ordinal))
&& (line.EndsWith(".fs", StringComparison.Ordinal)
|| line.EndsWith(".fsx", StringComparison.Ordinal)
|| line.EndsWith(".fsi", StringComparison.Ordinal))
then
Some(line.Replace("AM ", "").Replace("M ", ""))
else
Expand Down Expand Up @@ -372,7 +375,7 @@ let mkGithubRelease (v: SemanticVersion, d: DateTime, cd: ChangelogData option)
None
else
let output = cmdResult.StandardOutput.Trim()
let lastIdx = output.LastIndexOf("Z")
let lastIdx = output.LastIndexOf("Z", StringComparison.Ordinal)
Some(output.Substring(0, lastIdx))

let sections =
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/end-users/GettingStarted.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Consider the following PowerShell script:
function Format-Changed(){
$files =
git status --porcelain `
| Where-Object { ($_.StartsWith(" M") -or $_.StartsWith("AM")) `
| Where-Object { ($_.StartsWith(" M", "Ordinal") -or $_.StartsWith("AM", "Ordinal")) `
-and (Test-FSharpExtension $_) } | ForEach-Object { $_.substring(3) }
& "dotnet" "fantomas" $files
}
Expand Down
7 changes: 2 additions & 5 deletions src/Fantomas.Client/Contracts.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module Fantomas.Client.Contracts

open System
open System.Collections.Generic
open System.Globalization
open System.Threading
open System.Threading.Tasks

Expand All @@ -26,8 +25,7 @@ type FormatDocumentRequest =
Config: IReadOnlyDictionary<string, string> option
Cursor: FormatCursorPosition option }

member this.IsSignatureFile =
this.FilePath.EndsWith(".fsi", false, CultureInfo.InvariantCulture)
member this.IsSignatureFile = this.FilePath.EndsWith(".fsi", StringComparison.Ordinal)

and FormatCursorPosition =
class
Expand All @@ -49,8 +47,7 @@ type FormatSelectionRequest =
Range: FormatSelectionRange
}

member this.IsSignatureFile =
this.FilePath.EndsWith(".fsi", false, CultureInfo.InvariantCulture)
member this.IsSignatureFile = this.FilePath.EndsWith(".fsi", StringComparison.Ordinal)

and FormatSelectionRange =
class
Expand Down
24 changes: 7 additions & 17 deletions src/Fantomas.Core/ASTTransformer.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module internal rec Fantomas.Core.ASTTransformer

open System
open System.Collections.Generic
open System.Globalization
open System.Text.RegularExpressions
open Fantomas.FCS.Text
open Fantomas.FCS.Text.Range
Expand Down Expand Up @@ -486,9 +486,7 @@ let (|SameInfixApps|_|) expr =
match expr with
| InfixApp(_, operator, _) ->
let isRight =
Set.exists
(fun (rOp: string) -> operator.Text.StartsWith(rOp, false, CultureInfo.InvariantCulture))
rightOperators
Set.exists (fun (rOp: string) -> String.startsWithOrdinal rOp operator.Text) rightOperators

let head, xs =
if isRight then
Expand Down Expand Up @@ -1500,11 +1498,9 @@ let mkExpr (creationAide: CreationAide) (e: SynExpr) : Expr =
stn
(creationAide.TextFromSource
(fun () ->
if idx = 0 && not (v.StartsWith("$", false, CultureInfo.InvariantCulture)) then
if idx = 0 && not (String.startsWithOrdinal "$" v) then
$"$\"%s{v}{{"
elif
idx = lastIndex && not (v.EndsWith("\"", false, CultureInfo.InvariantCulture))
then
elif idx = lastIndex && not (String.endsWithOrdinal "\"" v) then
$"}}%s{v}\""
else
$"}}{v}{{")
Expand Down Expand Up @@ -1538,10 +1534,7 @@ let mkExpr (creationAide: CreationAide) (e: SynExpr) : Expr =
let c3Node = stn (creationAide.TextFromSource (fun () -> c3) mC3) mC3

let dotText =
if
c1Node.Text.EndsWith(".", false, CultureInfo.InvariantCulture)
|| c2Node.Text.EndsWith(".", false, CultureInfo.InvariantCulture)
then
if String.endsWithOrdinal "." c1Node.Text || String.endsWithOrdinal "." c2Node.Text then
" .. "
else
".."
Expand All @@ -1556,7 +1549,7 @@ let mkExpr (creationAide: CreationAide) (e: SynExpr) : Expr =
let hasSpaces =
let rec (|AtomicExpr|_|) e =
match e with
| ConstNumberExpr(v, _) when v.StartsWith("-", false, CultureInfo.InvariantCulture) -> None
| ConstNumberExpr(v, _) when String.startsWithOrdinal "-" v -> None
| SynExpr.Ident _
| SynExpr.Const(SynConst.Int32 _, _)
| SynExpr.IndexRange(expr1 = Some(AtomicExpr _); expr2 = Some(AtomicExpr _))
Expand Down Expand Up @@ -1606,10 +1599,7 @@ let mkExprQuote creationAide isRaw e range : ExprQuoteNode =
let (|ParenStarSynIdent|_|) =
function
| IdentTrivia.OriginalNotationWithParen(lpr, originalNotation, rpr) ->
if
originalNotation.Length > 1
&& originalNotation.StartsWith("*", false, CultureInfo.InvariantCulture)
then
if originalNotation.Length > 1 && String.startsWithOrdinal "*" originalNotation then
Some(lpr, originalNotation, rpr)
else
None
Expand Down
18 changes: 6 additions & 12 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module internal rec Fantomas.Core.CodePrinter

open System
open System.Globalization
open Fantomas.Core.Context
open Fantomas.Core.SyntaxOak
open Microsoft.FSharp.Core.CompilerServices
Expand Down Expand Up @@ -751,9 +750,7 @@ let genExpr (e: Expr) =

match node.Expr with
| Expr.Constant _
| Expr.InterpolatedStringExpr _ when
not (node.Operator.Text.StartsWith("%", false, CultureInfo.InvariantCulture))
->
| Expr.InterpolatedStringExpr _ when not (String.startsWithOrdinal "%" node.Operator.Text) ->
genSingleTextNode node.Operator +> sepSpace +> genExpr node.Expr
| Expr.AppSingleParenArg appNode ->
genSingleTextNode node.Operator
Expand Down Expand Up @@ -1841,7 +1838,7 @@ let genArrayOrList (preferMultilineCramped: bool) (node: ExprArrayOrListNode) =
let (|YieldLikeExpr|_|) e =
match e with
| Expr.Single singleNode ->
if singleNode.Leading.Text.StartsWith("yield", false, CultureInfo.InvariantCulture) then
if String.startsWithOrdinal "yield" singleNode.Leading.Text then
Some e
else
None
Expand Down Expand Up @@ -2263,7 +2260,7 @@ let colGenericTypeParameters typeParameters =
coli sepComma typeParameters (fun idx t ->
let leadingSpace =
match t with
| Type.Var n when idx = 0 && n.Text.StartsWith("^", false, CultureInfo.InvariantCulture) -> sepSpace
| Type.Var n when idx = 0 && String.startsWithOrdinal "^" n.Text -> sepSpace
| _ -> sepNone

leadingSpace +> genType t)
Expand Down Expand Up @@ -2514,10 +2511,7 @@ let genPatLeftMiddleRight (node: PatLeftMiddleRight) =

let genTyparDecl (isFirstTypeParam: bool) (td: TyparDeclNode) =
genOnelinerAttributes td.Attributes
+> onlyIf
(isFirstTypeParam
&& td.TypeParameter.Text.StartsWith("^", false, CultureInfo.InvariantCulture))
sepSpace
+> onlyIf (isFirstTypeParam && String.startsWithOrdinal "^" td.TypeParameter.Text) sepSpace
+> genSingleTextNode td.TypeParameter
|> genNode td

Expand Down Expand Up @@ -3111,7 +3105,7 @@ let genType (t: Type) =
let addExtraSpace =
match node.Arguments with
| [] -> sepNone
| Type.Var node :: _ when node.Text.StartsWith("^", false, CultureInfo.InvariantCulture) -> sepSpace
| Type.Var node :: _ when String.startsWithOrdinal "^" node.Text -> sepSpace
| t :: _ -> addSpaceIfSynTypeStaticConstantHasAtSignBeforeString t

genType node.Identifier
Expand Down Expand Up @@ -3213,7 +3207,7 @@ let addSpaceIfSynTypeStaticConstantHasAtSignBeforeString (t: Type) =
match t with
| Type.StaticConstant sc ->
match sc with
| Constant.FromText node -> onlyIf (node.Text.StartsWith("@", false, CultureInfo.InvariantCulture)) sepSpace
| Constant.FromText node -> onlyIf (String.startsWithOrdinal "@" node.Text) sepSpace
| _ -> sepNone
| _ -> sepNone

Expand Down
9 changes: 2 additions & 7 deletions src/Fantomas.Core/Context.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module internal Fantomas.Core.Context

open System
open System.Globalization
open Fantomas.FCS.Text
open Fantomas.Core
open Fantomas.Core.SyntaxOak
Expand Down Expand Up @@ -536,11 +535,7 @@ let sepSpace (ctx: Context) =
(!- " ") ctx
else
match lastWriteEventOnLastLine ctx with
| Some w when
(w.EndsWith(" ", false, CultureInfo.InvariantCulture)
|| w.EndsWith(Environment.NewLine, false, CultureInfo.InvariantCulture))
->
ctx
| Some w when (String.endsWithOrdinal " " w || String.endsWithOrdinal Environment.NewLine w) -> ctx
| None -> ctx
| _ -> (!- " ") ctx

Expand Down Expand Up @@ -864,7 +859,7 @@ let sepColon (ctx: Context) =
defaultExpr ctx
else
match lastWriteEventOnLastLine ctx with
| Some w when w.EndsWith(" ", false, CultureInfo.InvariantCulture) -> !- ": " ctx
| Some w when String.endsWithOrdinal " " w -> !- ": " ctx
| None -> !- ": " ctx
| _ -> defaultExpr ctx

Expand Down
6 changes: 3 additions & 3 deletions src/Fantomas.Core/Trivia.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module internal Fantomas.Core.Trivia

open System.Globalization
open System
open Fantomas.FCS.Syntax
open Fantomas.FCS.SyntaxTrivia
open Fantomas.FCS.Text
Expand Down Expand Up @@ -47,9 +47,9 @@ let internal collectTriviaFromCodeComments
let content =
let trimmedLine = line.TrimStart(' ', ';')

if index = 0 && trimmedLine.StartsWith("#!", false, CultureInfo.InvariantCulture) then // shebang
if index = 0 && String.startsWithOrdinal "#!" trimmedLine then // shebang
CommentOnSingleLine content
else if trimmedLine.StartsWith("//", false, CultureInfo.InvariantCulture) then
else if String.startsWithOrdinal "//" trimmedLine then
CommentOnSingleLine content
else
LineCommentAfterSourceCode content
Expand Down
3 changes: 3 additions & 0 deletions src/Fantomas.Core/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module String =
let startsWithOrdinal (prefix: string) (str: string) =
str.StartsWith(prefix, StringComparison.Ordinal)

let endsWithOrdinal (postfix: string) (str: string) =
str.EndsWith(postfix, StringComparison.Ordinal)

let empty = String.Empty
let isNotNullOrEmpty = String.IsNullOrEmpty >> not
let isNotNullOrWhitespace = String.IsNullOrWhiteSpace >> not
Expand Down
1 change: 1 addition & 0 deletions src/Fantomas.Core/Utils.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Fantomas.Core
[<RequireQualifiedAccess>]
module String =
val startsWithOrdinal: prefix: string -> str: string -> bool
val endsWithOrdinal: postfix: string -> str: string -> bool
val empty: string
val isNotNullOrEmpty: (string -> bool)
val isNotNullOrWhitespace: (string -> bool)
Expand Down

0 comments on commit c7b3918

Please sign in to comment.