Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/fsharp/CheckFormatStrings.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ let mkFlexibleDecimalFormatTypar (g: TcGlobals) m =
let mkFlexibleFloatFormatTypar (g: TcGlobals) m =
mkFlexibleFormatTypar m [ g.float_ty; g.float32_ty; g.decimal_ty ] g.float_ty

let isDigit c = ('0' <= c && c <= '9')

type FormatInfoRegister =
{ mutable leftJustify : bool
mutable numPrefixIfPos : char option
Expand Down Expand Up @@ -117,13 +115,13 @@ let parseFormatStringInternal (m:range) (g: TcGlobals) (context: FormatStringChe
let rec digitsPrecision i =
if i >= len then failwithf "%s" <| FSComp.SR.forBadPrecision()
match fmt.[i] with
| c when isDigit c -> digitsPrecision (i+1)
| c when System.Char.IsDigit c -> digitsPrecision (i+1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, open the namespace instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More often than not we use System.Char.IsDigit in this codebase. It's not clear to me why.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And amusingly @forki's PR does it both ways.

| _ -> i

let precision i =
if i >= len then failwithf "%s" <| FSComp.SR.forBadWidth()
match fmt.[i] with
| c when isDigit c -> info.precision <- true; false,digitsPrecision (i+1)
| c when System.Char.IsDigit c -> info.precision <- true; false,digitsPrecision (i+1)
| '*' -> info.precision <- true; true,(i+1)
| _ -> failwithf "%s" <| FSComp.SR.forPrecisionMissingAfterDot()

Expand All @@ -136,20 +134,20 @@ let parseFormatStringInternal (m:range) (g: TcGlobals) (context: FormatStringChe
let rec digitsWidthAndPrecision i =
if i >= len then failwithf "%s" <| FSComp.SR.forBadPrecision()
match fmt.[i] with
| c when isDigit c -> digitsWidthAndPrecision (i+1)
| c when System.Char.IsDigit c -> digitsWidthAndPrecision (i+1)
| _ -> optionalDotAndPrecision i

let widthAndPrecision i =
if i >= len then failwithf "%s" <| FSComp.SR.forBadPrecision()
match fmt.[i] with
| c when isDigit c -> false,digitsWidthAndPrecision i
| c when System.Char.IsDigit c -> false,digitsWidthAndPrecision i
| '*' -> true,optionalDotAndPrecision (i+1)
| _ -> false,optionalDotAndPrecision i

let rec digitsPosition n i =
if i >= len then failwithf "%s" <| FSComp.SR.forBadPrecision()
match fmt.[i] with
| c when isDigit c -> digitsPosition (n*10 + int c - int '0') (i+1)
| c when System.Char.IsDigit c -> digitsPosition (n*10 + int c - int '0') (i+1)
| '$' -> Some n, i+1
| _ -> None, i

Expand Down
10 changes: 4 additions & 6 deletions src/fsharp/FSharp.Core/printf.fs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,9 @@ module internal PrintfImpl =
/// Set of helpers to parse format string
module private FormatString =

let inline isDigit c = c >= '0' && c <= '9'

let intFromString (s: string) pos =
let intFromString (s: string) pos =
let rec go acc i =
if isDigit s.[i] then
if Char.IsDigit s.[i] then
let n = int s.[i] - int '0'
go (acc * 10 + n) (i + 1)
else acc, i
Expand All @@ -125,13 +123,13 @@ module internal PrintfImpl =

let parseWidth (s: string) i =
if s.[i] = '*' then StarValue, (i + 1)
elif isDigit (s.[i]) then intFromString s i
elif Char.IsDigit s.[i] then intFromString s i
else NotSpecifiedValue, i

let parsePrecision (s: string) i =
if s.[i] = '.' then
if s.[i + 1] = '*' then StarValue, i + 2
elif isDigit (s.[i + 1]) then intFromString s (i + 1)
elif Char.IsDigit s.[i + 1] then intFromString s (i + 1)
else raise (ArgumentException("invalid precision value"))
else NotSpecifiedValue, i

Expand Down