Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(612)

Unified Diff: Source/core/css/CSSPrimitiveValue.cpp

Issue 1319453002: Experiment: Switch to code-driven conversion of CSS unit type names. Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: drop redundant namespace prefix Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/core/css/CSSPrimitiveValue.h ('k') | Source/core/css/parser/CSSParserToken.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/css/CSSPrimitiveValue.cpp
diff --git a/Source/core/css/CSSPrimitiveValue.cpp b/Source/core/css/CSSPrimitiveValue.cpp
index 1eb2d16ebea7f91ff3144031a9868585198db0f8..87721ba21c1653d3689916bf5af09bc00509ceda 100644
--- a/Source/core/css/CSSPrimitiveValue.cpp
+++ b/Source/core/css/CSSPrimitiveValue.cpp
@@ -51,47 +51,6 @@ namespace {
const int maxValueForCssLength = INT_MAX / kFixedPointDenominator - 2;
const int minValueForCssLength = INT_MIN / kFixedPointDenominator + 2;
-typedef HashMap<String, CSSPrimitiveValue::UnitType> StringToUnitTable;
-
-StringToUnitTable createStringToUnitTable()
-{
- StringToUnitTable table;
- table.set(String("em"), CSSPrimitiveValue::UnitType::Ems);
- table.set(String("ex"), CSSPrimitiveValue::UnitType::Exs);
- table.set(String("px"), CSSPrimitiveValue::UnitType::Pixels);
- table.set(String("cm"), CSSPrimitiveValue::UnitType::Centimeters);
- table.set(String("mm"), CSSPrimitiveValue::UnitType::Millimeters);
- table.set(String("in"), CSSPrimitiveValue::UnitType::Inches);
- table.set(String("pt"), CSSPrimitiveValue::UnitType::Points);
- table.set(String("pc"), CSSPrimitiveValue::UnitType::Picas);
- table.set(String("deg"), CSSPrimitiveValue::UnitType::Degrees);
- table.set(String("rad"), CSSPrimitiveValue::UnitType::Radians);
- table.set(String("grad"), CSSPrimitiveValue::UnitType::Gradians);
- table.set(String("ms"), CSSPrimitiveValue::UnitType::Milliseconds);
- table.set(String("s"), CSSPrimitiveValue::UnitType::Seconds);
- table.set(String("hz"), CSSPrimitiveValue::UnitType::Hertz);
- table.set(String("khz"), CSSPrimitiveValue::UnitType::Kilohertz);
- table.set(String("dpi"), CSSPrimitiveValue::UnitType::DotsPerInch);
- table.set(String("dpcm"), CSSPrimitiveValue::UnitType::DotsPerCentimeter);
- table.set(String("dppx"), CSSPrimitiveValue::UnitType::DotsPerPixel);
- table.set(String("vw"), CSSPrimitiveValue::UnitType::ViewportWidth);
- table.set(String("vh"), CSSPrimitiveValue::UnitType::ViewportHeight);
- table.set(String("vmin"), CSSPrimitiveValue::UnitType::ViewportMin);
- table.set(String("vmax"), CSSPrimitiveValue::UnitType::ViewportMax);
- table.set(String("rem"), CSSPrimitiveValue::UnitType::Rems);
- table.set(String("fr"), CSSPrimitiveValue::UnitType::Fraction);
- table.set(String("turn"), CSSPrimitiveValue::UnitType::Turns);
- table.set(String("ch"), CSSPrimitiveValue::UnitType::Chs);
- table.set(String("__qem"), CSSPrimitiveValue::UnitType::QuirkyEms);
- return table;
-}
-
-StringToUnitTable& unitTable()
-{
- DEFINE_STATIC_LOCAL(StringToUnitTable, unitTable, (createStringToUnitTable()));
- return unitTable;
-}
-
} // namespace
float CSSPrimitiveValue::clampToCSSLengthRange(double value)
@@ -99,18 +58,100 @@ float CSSPrimitiveValue::clampToCSSLengthRange(double value)
return clampTo<float>(value, minValueForCssLength, maxValueForCssLength);
}
-void CSSPrimitiveValue::initUnitTable()
+// Intentionally avoid keeping a hashed map of short strings here, it requiring temporary
+// strings to be constructed and hashed per lookup. Instead, use code-driven lookup of
+// unit type values from their short names.
+
+CSSPrimitiveValue::UnitType CSSPrimitiveValue::fromName(const UChar* unit, unsigned length)
{
- // Make sure we initialize this during blink initialization
- // to avoid racy static local initialization.
- unitTable();
+ if (!length || length > 5)
+ return CSSPrimitiveValue::UnitType::Unknown;
+
+ LChar buffer[5];
+ for (unsigned i = 0; i < length; ++i) {
+ if (!isASCII(unit[i]))
+ return CSSPrimitiveValue::UnitType::Unknown;
+ buffer[i] = static_cast<LChar>(unit[i]);
+ }
+ return fromName(buffer, length);
}
-CSSPrimitiveValue::UnitType CSSPrimitiveValue::fromName(const String& unit)
+#define UNIT_MATCH_(index, ch, value) if (length == index + 1 && isASCIIAlphaCaselessEqual(unit[index], ch)) return value
+#define UNIT_MATCH2_(index, ch1, ch2, value) if (length == index + 2 && isASCIIAlphaCaselessEqual(unit[index], ch1) && isASCIIAlphaCaselessEqual(unit[index + 1], ch2)) return value
+#define UNIT_MATCH3_(index, ch1, ch2, ch3, value) if (length == index + 3 && isASCIIAlphaCaselessEqual(unit[index], ch1) && isASCIIAlphaCaselessEqual(unit[index + 1], ch2) && isASCIIAlphaCaselessEqual(unit[index + 2], ch3)) return value
+
+CSSPrimitiveValue::UnitType CSSPrimitiveValue::fromName(const LChar* unit, unsigned length)
{
- return unitTable().get(unit.lower());
+ if (!length || length > 5)
+ return UnitType::Unknown;
+
+ switch (toASCIILower(unit[0])) {
+ case '_':
+ if (length == 5 && unit[1] == '_')
+ UNIT_MATCH3_(2, 'q', 'e', 'm', UnitType::QuirkyEms);
+ return UnitType::Unknown;
+ case 'c':
+ UNIT_MATCH_(1, 'h', UnitType::Chs);
+ UNIT_MATCH_(1, 'm', UnitType::Centimeters);
+ return UnitType::Unknown;
+ case 'd':
+ UNIT_MATCH2_(1, 'e', 'g', UnitType::Degrees);
+ UNIT_MATCH2_(1, 'p', 'i', UnitType::DotsPerInch);
+ UNIT_MATCH3_(1, 'p', 'c', 'm', UnitType::DotsPerCentimeter);
+ UNIT_MATCH3_(1, 'p', 'p', 'x', UnitType::DotsPerPixel);
+ return UnitType::Unknown;
+ case 'e':
+ UNIT_MATCH_(1, 'm', UnitType::Ems);
+ UNIT_MATCH_(1, 'x', UnitType::Exs);
+ return UnitType::Unknown;
+ case 'f':
+ UNIT_MATCH_(1, 'r', UnitType::Fraction);
+ return UnitType::Unknown;
+ case 'g':
+ UNIT_MATCH3_(1, 'r', 'a', 'd', UnitType::Gradians);
+ return UnitType::Unknown;
+ case 'h':
+ UNIT_MATCH_(1, 'z', UnitType::Hertz);
+ return UnitType::Unknown;
+ case 'i':
+ UNIT_MATCH_(1, 'n', UnitType::Inches);
+ return UnitType::Unknown;
+ case 'k':
+ UNIT_MATCH2_(1, 'h', 'z', UnitType::Kilohertz);
+ return UnitType::Unknown;
+ case 'm':
+ UNIT_MATCH_(1, 'm', UnitType::Millimeters);
+ UNIT_MATCH_(1, 's', UnitType::Milliseconds);
+ return UnitType::Unknown;
+ case 'p':
+ UNIT_MATCH_(1, 'c', UnitType::Picas);
+ UNIT_MATCH_(1, 't', UnitType::Points);
+ UNIT_MATCH_(1, 'x', UnitType::Pixels);
+ return UnitType::Unknown;
+ case 'r':
+ UNIT_MATCH2_(1, 'a', 'd', UnitType::Radians);
+ UNIT_MATCH2_(1, 'e', 'm', UnitType::Rems);
+ return UnitType::Unknown;
+ case 's':
+ return UnitType::Seconds;
+ case 't':
+ UNIT_MATCH3_(1, 'u', 'r', 'n', UnitType::Turns);
+ return UnitType::Unknown;
+ case 'v':
+ UNIT_MATCH_(1, 'h', UnitType::ViewportHeight);
+ UNIT_MATCH_(1, 'w', UnitType::ViewportWidth);
+ UNIT_MATCH3_(1, 'm', 'a', 'x', UnitType::ViewportMax);
+ UNIT_MATCH3_(1, 'm', 'i', 'n', UnitType::ViewportMin);
+ return UnitType::Unknown;
+ default:
+ return UnitType::Unknown;
+ }
}
+#undef UNIT_MATCH_
+#undef UNIT_MATCH2_
+#undef UNIT_MATCH3_
+
CSSPrimitiveValue::UnitCategory CSSPrimitiveValue::unitCategory(UnitType type)
{
switch (type) {
« no previous file with comments | « Source/core/css/CSSPrimitiveValue.h ('k') | Source/core/css/parser/CSSParserToken.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698