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

Side by Side 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, 3 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org) 2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2012 Apple Inc. All rights reserv ed. 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2012 Apple Inc. All rights reserv ed.
4 * 4 *
5 * This library is free software; you can redistribute it and/or 5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public 6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either 7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version. 8 * version 2 of the License, or (at your option) any later version.
9 * 9 *
10 * This library is distributed in the hope that it will be useful, 10 * This library is distributed in the hope that it will be useful,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 44
45 namespace blink { 45 namespace blink {
46 46
47 namespace { 47 namespace {
48 48
49 // Max/min values for CSS, needs to slightly smaller/larger than the true max/mi n values to allow for rounding without overflowing. 49 // Max/min values for CSS, needs to slightly smaller/larger than the true max/mi n values to allow for rounding without overflowing.
50 // Subtract two (rather than one) to allow for values to be converted to float a nd back without exceeding the LayoutUnit::max. 50 // Subtract two (rather than one) to allow for values to be converted to float a nd back without exceeding the LayoutUnit::max.
51 const int maxValueForCssLength = INT_MAX / kFixedPointDenominator - 2; 51 const int maxValueForCssLength = INT_MAX / kFixedPointDenominator - 2;
52 const int minValueForCssLength = INT_MIN / kFixedPointDenominator + 2; 52 const int minValueForCssLength = INT_MIN / kFixedPointDenominator + 2;
53 53
54 typedef HashMap<String, CSSPrimitiveValue::UnitType> StringToUnitTable;
55
56 StringToUnitTable createStringToUnitTable()
57 {
58 StringToUnitTable table;
59 table.set(String("em"), CSSPrimitiveValue::UnitType::Ems);
60 table.set(String("ex"), CSSPrimitiveValue::UnitType::Exs);
61 table.set(String("px"), CSSPrimitiveValue::UnitType::Pixels);
62 table.set(String("cm"), CSSPrimitiveValue::UnitType::Centimeters);
63 table.set(String("mm"), CSSPrimitiveValue::UnitType::Millimeters);
64 table.set(String("in"), CSSPrimitiveValue::UnitType::Inches);
65 table.set(String("pt"), CSSPrimitiveValue::UnitType::Points);
66 table.set(String("pc"), CSSPrimitiveValue::UnitType::Picas);
67 table.set(String("deg"), CSSPrimitiveValue::UnitType::Degrees);
68 table.set(String("rad"), CSSPrimitiveValue::UnitType::Radians);
69 table.set(String("grad"), CSSPrimitiveValue::UnitType::Gradians);
70 table.set(String("ms"), CSSPrimitiveValue::UnitType::Milliseconds);
71 table.set(String("s"), CSSPrimitiveValue::UnitType::Seconds);
72 table.set(String("hz"), CSSPrimitiveValue::UnitType::Hertz);
73 table.set(String("khz"), CSSPrimitiveValue::UnitType::Kilohertz);
74 table.set(String("dpi"), CSSPrimitiveValue::UnitType::DotsPerInch);
75 table.set(String("dpcm"), CSSPrimitiveValue::UnitType::DotsPerCentimeter);
76 table.set(String("dppx"), CSSPrimitiveValue::UnitType::DotsPerPixel);
77 table.set(String("vw"), CSSPrimitiveValue::UnitType::ViewportWidth);
78 table.set(String("vh"), CSSPrimitiveValue::UnitType::ViewportHeight);
79 table.set(String("vmin"), CSSPrimitiveValue::UnitType::ViewportMin);
80 table.set(String("vmax"), CSSPrimitiveValue::UnitType::ViewportMax);
81 table.set(String("rem"), CSSPrimitiveValue::UnitType::Rems);
82 table.set(String("fr"), CSSPrimitiveValue::UnitType::Fraction);
83 table.set(String("turn"), CSSPrimitiveValue::UnitType::Turns);
84 table.set(String("ch"), CSSPrimitiveValue::UnitType::Chs);
85 table.set(String("__qem"), CSSPrimitiveValue::UnitType::QuirkyEms);
86 return table;
87 }
88
89 StringToUnitTable& unitTable()
90 {
91 DEFINE_STATIC_LOCAL(StringToUnitTable, unitTable, (createStringToUnitTable() ));
92 return unitTable;
93 }
94
95 } // namespace 54 } // namespace
96 55
97 float CSSPrimitiveValue::clampToCSSLengthRange(double value) 56 float CSSPrimitiveValue::clampToCSSLengthRange(double value)
98 { 57 {
99 return clampTo<float>(value, minValueForCssLength, maxValueForCssLength); 58 return clampTo<float>(value, minValueForCssLength, maxValueForCssLength);
100 } 59 }
101 60
102 void CSSPrimitiveValue::initUnitTable() 61 // Intentionally avoid keeping a hashed map of short strings here, it requiring temporary
62 // strings to be constructed and hashed per lookup. Instead, use code-driven loo kup of
63 // unit type values from their short names.
64
65 CSSPrimitiveValue::UnitType CSSPrimitiveValue::fromName(const UChar* unit, unsig ned length)
103 { 66 {
104 // Make sure we initialize this during blink initialization 67 if (!length || length > 5)
105 // to avoid racy static local initialization. 68 return CSSPrimitiveValue::UnitType::Unknown;
106 unitTable(); 69
70 LChar buffer[5];
71 for (unsigned i = 0; i < length; ++i) {
72 if (!isASCII(unit[i]))
73 return CSSPrimitiveValue::UnitType::Unknown;
74 buffer[i] = static_cast<LChar>(unit[i]);
75 }
76 return fromName(buffer, length);
107 } 77 }
108 78
109 CSSPrimitiveValue::UnitType CSSPrimitiveValue::fromName(const String& unit) 79 #define UNIT_MATCH_(index, ch, value) if (length == index + 1 && isASCIIAlphaCas elessEqual(unit[index], ch)) return value
80 #define UNIT_MATCH2_(index, ch1, ch2, value) if (length == index + 2 && isASCIIA lphaCaselessEqual(unit[index], ch1) && isASCIIAlphaCaselessEqual(unit[index + 1] , ch2)) return value
81 #define UNIT_MATCH3_(index, ch1, ch2, ch3, value) if (length == index + 3 && isA SCIIAlphaCaselessEqual(unit[index], ch1) && isASCIIAlphaCaselessEqual(unit[index + 1], ch2) && isASCIIAlphaCaselessEqual(unit[index + 2], ch3)) return value
82
83 CSSPrimitiveValue::UnitType CSSPrimitiveValue::fromName(const LChar* unit, unsig ned length)
110 { 84 {
111 return unitTable().get(unit.lower()); 85 if (!length || length > 5)
86 return UnitType::Unknown;
87
88 switch (toASCIILower(unit[0])) {
89 case '_':
90 if (length == 5 && unit[1] == '_')
91 UNIT_MATCH3_(2, 'q', 'e', 'm', UnitType::QuirkyEms);
92 return UnitType::Unknown;
93 case 'c':
94 UNIT_MATCH_(1, 'h', UnitType::Chs);
95 UNIT_MATCH_(1, 'm', UnitType::Centimeters);
96 return UnitType::Unknown;
97 case 'd':
98 UNIT_MATCH2_(1, 'e', 'g', UnitType::Degrees);
99 UNIT_MATCH2_(1, 'p', 'i', UnitType::DotsPerInch);
100 UNIT_MATCH3_(1, 'p', 'c', 'm', UnitType::DotsPerCentimeter);
101 UNIT_MATCH3_(1, 'p', 'p', 'x', UnitType::DotsPerPixel);
102 return UnitType::Unknown;
103 case 'e':
104 UNIT_MATCH_(1, 'm', UnitType::Ems);
105 UNIT_MATCH_(1, 'x', UnitType::Exs);
106 return UnitType::Unknown;
107 case 'f':
108 UNIT_MATCH_(1, 'r', UnitType::Fraction);
109 return UnitType::Unknown;
110 case 'g':
111 UNIT_MATCH3_(1, 'r', 'a', 'd', UnitType::Gradians);
112 return UnitType::Unknown;
113 case 'h':
114 UNIT_MATCH_(1, 'z', UnitType::Hertz);
115 return UnitType::Unknown;
116 case 'i':
117 UNIT_MATCH_(1, 'n', UnitType::Inches);
118 return UnitType::Unknown;
119 case 'k':
120 UNIT_MATCH2_(1, 'h', 'z', UnitType::Kilohertz);
121 return UnitType::Unknown;
122 case 'm':
123 UNIT_MATCH_(1, 'm', UnitType::Millimeters);
124 UNIT_MATCH_(1, 's', UnitType::Milliseconds);
125 return UnitType::Unknown;
126 case 'p':
127 UNIT_MATCH_(1, 'c', UnitType::Picas);
128 UNIT_MATCH_(1, 't', UnitType::Points);
129 UNIT_MATCH_(1, 'x', UnitType::Pixels);
130 return UnitType::Unknown;
131 case 'r':
132 UNIT_MATCH2_(1, 'a', 'd', UnitType::Radians);
133 UNIT_MATCH2_(1, 'e', 'm', UnitType::Rems);
134 return UnitType::Unknown;
135 case 's':
136 return UnitType::Seconds;
137 case 't':
138 UNIT_MATCH3_(1, 'u', 'r', 'n', UnitType::Turns);
139 return UnitType::Unknown;
140 case 'v':
141 UNIT_MATCH_(1, 'h', UnitType::ViewportHeight);
142 UNIT_MATCH_(1, 'w', UnitType::ViewportWidth);
143 UNIT_MATCH3_(1, 'm', 'a', 'x', UnitType::ViewportMax);
144 UNIT_MATCH3_(1, 'm', 'i', 'n', UnitType::ViewportMin);
145 return UnitType::Unknown;
146 default:
147 return UnitType::Unknown;
148 }
112 } 149 }
113 150
151 #undef UNIT_MATCH_
152 #undef UNIT_MATCH2_
153 #undef UNIT_MATCH3_
154
114 CSSPrimitiveValue::UnitCategory CSSPrimitiveValue::unitCategory(UnitType type) 155 CSSPrimitiveValue::UnitCategory CSSPrimitiveValue::unitCategory(UnitType type)
115 { 156 {
116 switch (type) { 157 switch (type) {
117 case UnitType::Number: 158 case UnitType::Number:
118 return CSSPrimitiveValue::UNumber; 159 return CSSPrimitiveValue::UNumber;
119 case UnitType::Percentage: 160 case UnitType::Percentage:
120 return CSSPrimitiveValue::UPercent; 161 return CSSPrimitiveValue::UPercent;
121 case UnitType::Pixels: 162 case UnitType::Pixels:
122 case UnitType::Centimeters: 163 case UnitType::Centimeters:
123 case UnitType::Millimeters: 164 case UnitType::Millimeters:
(...skipping 1029 matching lines...) Expand 10 before | Expand all | Expand 10 after
1153 visitor->trace(m_value.shape); 1194 visitor->trace(m_value.shape);
1154 break; 1195 break;
1155 default: 1196 default:
1156 break; 1197 break;
1157 } 1198 }
1158 #endif 1199 #endif
1159 CSSValue::traceAfterDispatch(visitor); 1200 CSSValue::traceAfterDispatch(visitor);
1160 } 1201 }
1161 1202
1162 } // namespace blink 1203 } // namespace blink
OLDNEW
« 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