From 7df64a0a23d4ce6f7f3f5f77abd154711dab6e92 Mon Sep 17 00:00:00 2001 From: Alfonso Garcia-Caro Date: Tue, 28 Aug 2018 22:56:38 +0200 Subject: [PATCH] Use getUnicodeCategory for Char/isWhitespace --- src/js/fable-core/Char.ts | 11 ++++++++++- tests/Main/CharTests.fs | 7 ++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/js/fable-core/Char.ts b/src/js/fable-core/Char.ts index 3d78a7aaaf..9933effd40 100644 --- a/src/js/fable-core/Char.ts +++ b/src/js/fable-core/Char.ts @@ -117,6 +117,10 @@ const isSymbolMask = 0 | 1 << UnicodeCategory.CurrencySymbol | 1 << UnicodeCategory.ModifierSymbol | 1 << UnicodeCategory.OtherSymbol; +const isWhiteSpaceMask = 0 + | 1 << UnicodeCategory.SpaceSeparator + | 1 << UnicodeCategory.LineSeparator + | 1 << UnicodeCategory.ParagraphSeparator; export const getUnicodeCategory = getCategory(); @@ -171,7 +175,12 @@ export function isSymbol(s: string, index?: number) { } export function isWhiteSpace(s: string, index?: number) { - return /[\s\x09-\x0D\x85\xA0]/.test(s.charAt(index || 0)); + const test = 1 << getUnicodeCategory(s, index); + if ((test & isWhiteSpaceMask) !== 0) { + return true; + } + const cp = s.charCodeAt(index || 0); + return (0x09 <= cp && cp <= 0x0D) || cp === 0x85 || cp === 0xA0; } export function isHighSurrogate(s: string, index?: number) { diff --git a/tests/Main/CharTests.fs b/tests/Main/CharTests.fs index 7019d50a0e..aa98c16d38 100644 --- a/tests/Main/CharTests.fs +++ b/tests/Main/CharTests.fs @@ -37,7 +37,7 @@ let tests = Char.GetUnicodeCategory(str,2) |> int |> equal 8 //UnicodeCategory.DecimalDigitNumber testCase "Char.IsControl works" <| fun () -> - Char.IsControl('a') |> equal false + Char.IsControl('a') |> equal false Char.IsControl('\u0000') |> equal true Char.IsControl('\u001F') |> equal true Char.IsControl('\u007F') |> equal true @@ -142,6 +142,11 @@ let tests = Char.IsWhiteSpace(' ') |> equal true Char.IsWhiteSpace('\n') |> equal true Char.IsWhiteSpace('\t') |> equal true + Char.IsWhiteSpace('\009') |> equal true + Char.IsWhiteSpace('\013') |> equal true + Char.IsWhiteSpace('\133') |> equal true + Char.IsWhiteSpace('\160') |> equal true + Char.IsWhiteSpace('-') |> equal false testCase "Char.IsWhitespace works with two args" <| fun () -> let input = " \r"