From 0f0dffb24e85780dee0ff824a3375d707aa80d89 Mon Sep 17 00:00:00 2001 From: Jan de Mooij Date: Wed, 22 Apr 2015 11:30:47 +0200 Subject: [PATCH] Bug 1156886 - Optimize toLowerCase and toUpperCase on ASCII characters. r=luke --- js/src/jsstr.cpp | 4 ++-- js/src/vm/Unicode.h | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/js/src/jsstr.cpp b/js/src/jsstr.cpp index 1cd2be6b8e7..7752217af04 100644 --- a/js/src/jsstr.cpp +++ b/js/src/jsstr.cpp @@ -603,7 +603,7 @@ ToLowerCase(JSContext* cx, JSLinearString* str) size_t i = 0; for (; i < length; i++) { char16_t c = chars[i]; - if (unicode::ToLowerCase(c) != c) + if (unicode::CanLowerCase(c)) break; } @@ -722,7 +722,7 @@ ToUpperCase(JSContext* cx, JSLinearString* str) size_t i = 0; for (; i < length; i++) { char16_t c = chars[i]; - if (unicode::ToUpperCase(c) != c) + if (unicode::CanUpperCase(c)) break; } diff --git a/js/src/vm/Unicode.h b/js/src/vm/Unicode.h index 591e46f56e2..ea853442c7f 100644 --- a/js/src/vm/Unicode.h +++ b/js/src/vm/Unicode.h @@ -191,6 +191,12 @@ IsSpaceOrBOM2(char16_t ch) inline char16_t ToUpperCase(char16_t ch) { + if (ch < 128) { + if (ch >= 'a' && ch <= 'z') + return ch - ('a' - 'A'); + return ch; + } + const CharacterInfo& info = CharInfo(ch); return uint16_t(ch) + info.upperCase; @@ -199,11 +205,35 @@ ToUpperCase(char16_t ch) inline char16_t ToLowerCase(char16_t ch) { + if (ch < 128) { + if (ch >= 'A' && ch <= 'Z') + return ch + ('a' - 'A'); + return ch; + } + const CharacterInfo& info = CharInfo(ch); return uint16_t(ch) + info.lowerCase; } +// Returns true iff ToUpperCase(ch) != ch. +inline bool +CanUpperCase(char16_t ch) +{ + if (ch < 128) + return ch >= 'a' && ch <= 'z'; + return CharInfo(ch).upperCase != 0; +} + +// Returns true iff ToLowerCase(ch) != ch. +inline bool +CanLowerCase(char16_t ch) +{ + if (ch < 128) + return ch >= 'A' && ch <= 'Z'; + return CharInfo(ch).lowerCase != 0; +} + } /* namespace unicode */ } /* namespace js */