Skip to content

Commit

Permalink
Use getUnicodeCategory for Char/isWhitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsogarciacaro committed Aug 28, 2018
1 parent d3f79cd commit 7df64a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/js/fable-core/Char.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion tests/Main/CharTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 7df64a0

Please sign in to comment.