File: | var/lib/jenkins/workspace/firefox-scan-build/layout/mathml/nsMathMLOperators.cpp |
Warning: | line 163, column 13 Value stored to 'end' during its initialization is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | |
7 | #include "nsMathMLOperators.h" |
8 | #include "nsCOMPtr.h" |
9 | #include "nsTHashMap.h" |
10 | #include "nsHashKeys.h" |
11 | #include "nsNetUtil.h" |
12 | #include "nsTArray.h" |
13 | |
14 | #include "mozilla/intl/UnicodeProperties.h" |
15 | #include "nsIPersistentProperties2.h" |
16 | #include "nsISimpleEnumerator.h" |
17 | #include "nsCRT.h" |
18 | |
19 | // operator dictionary entry |
20 | struct OperatorData { |
21 | OperatorData(void) : mFlags(0), mLeadingSpace(0.0f), mTrailingSpace(0.0f) {} |
22 | |
23 | // member data |
24 | nsString mStr; |
25 | nsOperatorFlags mFlags; |
26 | float mLeadingSpace; // unit is em |
27 | float mTrailingSpace; // unit is em |
28 | }; |
29 | |
30 | static int32_t gTableRefCount = 0; |
31 | static uint32_t gOperatorCount = 0; |
32 | static OperatorData* gOperatorArray = nullptr; |
33 | static nsTHashMap<nsStringHashKey, OperatorData*>* gOperatorTable = nullptr; |
34 | static bool gGlobalsInitialized = false; |
35 | |
36 | static const char16_t kDashCh = char16_t('#'); |
37 | static const char16_t kColonCh = char16_t(':'); |
38 | |
39 | static uint32_t ToUnicodeCodePoint(const nsString& aOperator) { |
40 | if (aOperator.Length() == 1) { |
41 | return aOperator[0]; |
42 | } |
43 | if (aOperator.Length() == 2 && |
44 | NS_IS_SURROGATE_PAIR(aOperator[0], aOperator[1])(((uint32_t(aOperator[0]) & 0xFFFFFC00) == 0xD800) && ((uint32_t(aOperator[1]) & 0xFFFFFC00) == 0xDC00))) { |
45 | return SURROGATE_TO_UCS4(aOperator[0], aOperator[1])(((uint32_t(aOperator[0]) & 0x03FF) << 10) + (uint32_t (aOperator[1]) & 0x03FF) + uint32_t(0x00010000)); |
46 | } |
47 | return 0; |
48 | } |
49 | |
50 | static void SetBooleanProperty(OperatorData* aOperatorData, nsString aName) { |
51 | if (aName.IsEmpty()) return; |
52 | |
53 | if (aName.EqualsLiteral("stretchy") && (1 == aOperatorData->mStr.Length())) |
54 | aOperatorData->mFlags |= NS_MATHML_OPERATOR_STRETCHY; |
55 | else if (aName.EqualsLiteral("fence")) |
56 | aOperatorData->mFlags |= NS_MATHML_OPERATOR_FENCE; |
57 | else if (aName.EqualsLiteral("accent")) |
58 | aOperatorData->mFlags |= NS_MATHML_OPERATOR_ACCENT; |
59 | else if (aName.EqualsLiteral("largeop")) |
60 | aOperatorData->mFlags |= NS_MATHML_OPERATOR_LARGEOP; |
61 | else if (aName.EqualsLiteral("separator")) |
62 | aOperatorData->mFlags |= NS_MATHML_OPERATOR_SEPARATOR; |
63 | else if (aName.EqualsLiteral("movablelimits")) |
64 | aOperatorData->mFlags |= NS_MATHML_OPERATOR_MOVABLELIMITS; |
65 | else if (aName.EqualsLiteral("symmetric")) |
66 | aOperatorData->mFlags |= NS_MATHML_OPERATOR_SYMMETRIC; |
67 | } |
68 | |
69 | static void SetProperty(OperatorData* aOperatorData, nsString aName, |
70 | nsString aValue) { |
71 | if (aName.IsEmpty() || aValue.IsEmpty()) return; |
72 | |
73 | if (aName.EqualsLiteral("direction")) { |
74 | if (aValue.EqualsLiteral("vertical")) |
75 | aOperatorData->mFlags |= NS_MATHML_OPERATOR_DIRECTION_VERTICAL; |
76 | else if (aValue.EqualsLiteral("horizontal")) |
77 | aOperatorData->mFlags |= NS_MATHML_OPERATOR_DIRECTION_HORIZONTAL; |
78 | else |
79 | return; // invalid value |
80 | } else { |
81 | bool isLeadingSpace; |
82 | if (aName.EqualsLiteral("lspace")) |
83 | isLeadingSpace = true; |
84 | else if (aName.EqualsLiteral("rspace")) |
85 | isLeadingSpace = false; |
86 | else |
87 | return; // input is not applicable |
88 | |
89 | // aValue is assumed to be a digit from 0 to 7 |
90 | nsresult error = NS_OK; |
91 | float space = aValue.ToFloat(&error) / 18.0; |
92 | if (NS_FAILED(error)((bool)(__builtin_expect(!!(NS_FAILED_impl(error)), 0)))) return; |
93 | |
94 | if (isLeadingSpace) |
95 | aOperatorData->mLeadingSpace = space; |
96 | else |
97 | aOperatorData->mTrailingSpace = space; |
98 | } |
99 | } |
100 | |
101 | static bool SetOperator(OperatorData* aOperatorData, nsOperatorFlags aForm, |
102 | const nsCString& aOperator, nsString& aAttributes) |
103 | |
104 | { |
105 | static const char16_t kNullCh = char16_t('\0'); |
106 | |
107 | // aOperator is in the expanded format \uNNNN\uNNNN ... |
108 | // First compress these Unicode points to the internal nsString format |
109 | int32_t i = 0; |
110 | nsAutoString name, value; |
111 | int32_t len = aOperator.Length(); |
112 | char16_t c = aOperator[i++]; |
113 | uint32_t state = 0; |
114 | char16_t uchar = 0; |
115 | while (i <= len) { |
116 | if (0 == state) { |
117 | if (c != '\\') return false; |
118 | if (i < len) c = aOperator[i]; |
119 | i++; |
120 | if (('u' != c) && ('U' != c)) return false; |
121 | if (i < len) c = aOperator[i]; |
122 | i++; |
123 | state++; |
124 | } else { |
125 | if (('0' <= c) && (c <= '9')) |
126 | uchar = (uchar << 4) | (c - '0'); |
127 | else if (('a' <= c) && (c <= 'f')) |
128 | uchar = (uchar << 4) | (c - 'a' + 0x0a); |
129 | else if (('A' <= c) && (c <= 'F')) |
130 | uchar = (uchar << 4) | (c - 'A' + 0x0a); |
131 | else |
132 | return false; |
133 | if (i < len) c = aOperator[i]; |
134 | i++; |
135 | state++; |
136 | if (5 == state) { |
137 | value.Append(uchar); |
138 | uchar = 0; |
139 | state = 0; |
140 | } |
141 | } |
142 | } |
143 | if (0 != state) return false; |
144 | |
145 | // Quick return when the caller doesn't care about the attributes and just |
146 | // wants to know if this is a valid operator (this is the case at the first |
147 | // pass of the parsing of the dictionary in InitOperators()) |
148 | if (!aForm) return true; |
149 | |
150 | // Add operator to hash table |
151 | aOperatorData->mFlags |= aForm; |
152 | aOperatorData->mStr.Assign(value); |
153 | value.AppendInt(aForm, 10); |
154 | gOperatorTable->InsertOrUpdate(value, aOperatorData); |
155 | |
156 | #ifdef DEBUG1 |
157 | NS_LossyConvertUTF16toASCII str(aAttributes); |
158 | #endif |
159 | // Loop over the space-delimited list of attributes to get the name:value |
160 | // pairs |
161 | aAttributes.Append(kNullCh); // put an extra null at the end |
162 | char16_t* start = aAttributes.BeginWriting(); |
163 | char16_t* end = start; |
Value stored to 'end' during its initialization is never read | |
164 | while ((kNullCh != *start) && (kDashCh != *start)) { |
165 | name.SetLength(0); |
166 | value.SetLength(0); |
167 | // skip leading space, the dash amounts to the end of the line |
168 | while ((kNullCh != *start) && (kDashCh != *start) && |
169 | nsCRT::IsAsciiSpace(*start)) { |
170 | ++start; |
171 | } |
172 | end = start; |
173 | // look for ':' |
174 | while ((kNullCh != *end) && (kDashCh != *end) && |
175 | !nsCRT::IsAsciiSpace(*end) && (kColonCh != *end)) { |
176 | ++end; |
177 | } |
178 | // If ':' is not found, then it's a boolean property |
179 | bool IsBooleanProperty = (kColonCh != *end); |
180 | *end = kNullCh; // end segment here |
181 | // this segment is the name |
182 | if (start < end) { |
183 | name.Assign(start); |
184 | } |
185 | if (IsBooleanProperty) { |
186 | SetBooleanProperty(aOperatorData, name); |
187 | } else { |
188 | start = ++end; |
189 | // look for space or end of line |
190 | while ((kNullCh != *end) && (kDashCh != *end) && |
191 | !nsCRT::IsAsciiSpace(*end)) { |
192 | ++end; |
193 | } |
194 | *end = kNullCh; // end segment here |
195 | if (start < end) { |
196 | // this segment is the value |
197 | value.Assign(start); |
198 | } |
199 | SetProperty(aOperatorData, name, value); |
200 | } |
201 | start = ++end; |
202 | } |
203 | return true; |
204 | } |
205 | |
206 | static nsresult InitOperators(void) { |
207 | // Load the property file containing the Operator Dictionary |
208 | nsresult rv; |
209 | nsCOMPtr<nsIPersistentProperties> mathfontProp; |
210 | rv = NS_LoadPersistentPropertiesFromURISpec( |
211 | getter_AddRefs(mathfontProp), |
212 | "resource://gre/res/fonts/mathfont.properties"_ns); |
213 | |
214 | if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return rv; |
215 | |
216 | // Parse the Operator Dictionary in two passes. |
217 | // The first pass is to count the number of operators; the second pass is to |
218 | // allocate the necessary space for them and to add them in the hash table. |
219 | for (int32_t pass = 1; pass <= 2; pass++) { |
220 | OperatorData dummyData; |
221 | OperatorData* operatorData = &dummyData; |
222 | nsCOMPtr<nsISimpleEnumerator> iterator; |
223 | if (NS_SUCCEEDED(mathfontProp->Enumerate(getter_AddRefs(iterator)))((bool)(__builtin_expect(!!(!NS_FAILED_impl(mathfontProp-> Enumerate(getter_AddRefs(iterator)))), 1)))) { |
224 | bool more; |
225 | uint32_t index = 0; |
226 | nsAutoCString name; |
227 | nsAutoString attributes; |
228 | while ((NS_SUCCEEDED(iterator->HasMoreElements(&more))((bool)(__builtin_expect(!!(!NS_FAILED_impl(iterator->HasMoreElements (&more))), 1)))) && more) { |
229 | nsCOMPtr<nsISupports> supports; |
230 | nsCOMPtr<nsIPropertyElement> element; |
231 | if (NS_SUCCEEDED(iterator->GetNext(getter_AddRefs(supports)))((bool)(__builtin_expect(!!(!NS_FAILED_impl(iterator->GetNext (getter_AddRefs(supports)))), 1)))) { |
232 | element = do_QueryInterface(supports); |
233 | if (NS_SUCCEEDED(element->GetKey(name))((bool)(__builtin_expect(!!(!NS_FAILED_impl(element->GetKey (name))), 1))) && |
234 | NS_SUCCEEDED(element->GetValue(attributes))((bool)(__builtin_expect(!!(!NS_FAILED_impl(element->GetValue (attributes))), 1)))) { |
235 | // expected key: operator.\uNNNN.{infix,postfix,prefix} |
236 | if ((21 <= name.Length()) && (0 == name.Find("operator.\\u"))) { |
237 | name.Cut(0, 9); // 9 is the length of "operator."; |
238 | int32_t len = name.Length(); |
239 | nsOperatorFlags form = 0; |
240 | if (kNotFound != name.RFind(".infix")) { |
241 | form = NS_MATHML_OPERATOR_FORM_INFIX; |
242 | len -= 6; // 6 is the length of ".infix"; |
243 | } else if (kNotFound != name.RFind(".postfix")) { |
244 | form = NS_MATHML_OPERATOR_FORM_POSTFIX; |
245 | len -= 8; // 8 is the length of ".postfix"; |
246 | } else if (kNotFound != name.RFind(".prefix")) { |
247 | form = NS_MATHML_OPERATOR_FORM_PREFIX; |
248 | len -= 7; // 7 is the length of ".prefix"; |
249 | } else |
250 | continue; // input is not applicable |
251 | name.SetLength(len); |
252 | if (2 == pass) { // allocate space and start the storage |
253 | if (!gOperatorArray) { |
254 | if (0 == gOperatorCount) return NS_ERROR_UNEXPECTED; |
255 | gOperatorArray = new OperatorData[gOperatorCount]; |
256 | } |
257 | operatorData = &gOperatorArray[index]; |
258 | } else { |
259 | form = 0; // to quickly return from SetOperator() at pass 1 |
260 | } |
261 | // See if the operator should be retained |
262 | if (SetOperator(operatorData, form, name, attributes)) { |
263 | index++; |
264 | if (1 == pass) gOperatorCount = index; |
265 | } |
266 | } |
267 | } |
268 | } |
269 | } |
270 | } |
271 | } |
272 | return NS_OK; |
273 | } |
274 | |
275 | static nsresult InitOperatorGlobals() { |
276 | gGlobalsInitialized = true; |
277 | nsresult rv = NS_ERROR_OUT_OF_MEMORY; |
278 | gOperatorTable = new nsTHashMap<nsStringHashKey, OperatorData*>(); |
279 | if (gOperatorTable) { |
280 | rv = InitOperators(); |
281 | } |
282 | if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) nsMathMLOperators::CleanUp(); |
283 | return rv; |
284 | } |
285 | |
286 | void nsMathMLOperators::CleanUp() { |
287 | if (gOperatorArray) { |
288 | delete[] gOperatorArray; |
289 | gOperatorArray = nullptr; |
290 | } |
291 | if (gOperatorTable) { |
292 | delete gOperatorTable; |
293 | gOperatorTable = nullptr; |
294 | } |
295 | } |
296 | |
297 | void nsMathMLOperators::AddRefTable(void) { gTableRefCount++; } |
298 | |
299 | void nsMathMLOperators::ReleaseTable(void) { |
300 | if (0 == --gTableRefCount) { |
301 | CleanUp(); |
302 | } |
303 | } |
304 | |
305 | static OperatorData* GetOperatorData(const nsString& aOperator, |
306 | const uint8_t aForm) { |
307 | nsAutoString key(aOperator); |
308 | key.AppendInt(aForm); |
309 | return gOperatorTable->Get(key); |
310 | } |
311 | |
312 | bool nsMathMLOperators::LookupOperator(const nsString& aOperator, |
313 | const uint8_t aForm, |
314 | nsOperatorFlags* aFlags, |
315 | float* aLeadingSpace, |
316 | float* aTrailingSpace) { |
317 | NS_ASSERTION(aFlags && aLeadingSpace && aTrailingSpace, "bad usage")do { if (!(aFlags && aLeadingSpace && aTrailingSpace )) { NS_DebugBreak(NS_DEBUG_ASSERTION, "bad usage", "aFlags && aLeadingSpace && aTrailingSpace" , "/var/lib/jenkins/workspace/firefox-scan-build/layout/mathml/nsMathMLOperators.cpp" , 317); MOZ_PretendNoReturn(); } } while (0); |
318 | NS_ASSERTION(aForm > 0 && aForm < 4, "*** invalid call ***")do { if (!(aForm > 0 && aForm < 4)) { NS_DebugBreak (NS_DEBUG_ASSERTION, "*** invalid call ***", "aForm > 0 && aForm < 4" , "/var/lib/jenkins/workspace/firefox-scan-build/layout/mathml/nsMathMLOperators.cpp" , 318); MOZ_PretendNoReturn(); } } while (0); |
319 | |
320 | // Operator strings must be of length 1 or 2 in UTF-16. |
321 | // https://w3c.github.io/mathml-core/#dfn-algorithm-to-determine-the-category-of-an-operator |
322 | if (aOperator.IsEmpty() || aOperator.Length() > 2) { |
323 | return false; |
324 | } |
325 | |
326 | if (aOperator.Length() == 2) { |
327 | // Try and handle Arabic operators. |
328 | // https://w3c.github.io/mathml-core/#dfn-algorithm-to-determine-the-category-of-an-operator |
329 | if (auto codePoint = ToUnicodeCodePoint(aOperator)) { |
330 | if (aForm == NS_MATHML_OPERATOR_FORM_POSTFIX && |
331 | (codePoint == 0x1EEF0 || codePoint == 0x1EEF1)) { |
332 | // Use category I. |
333 | // https://w3c.github.io/mathml-core/#operator-dictionary-categories-values |
334 | *aFlags = NS_MATHML_OPERATOR_FORM_POSTFIX | |
335 | NS_MATHML_OPERATOR_STRETCHY | |
336 | NS_MATHML_OPERATOR_DIRECTION_HORIZONTAL; |
337 | *aLeadingSpace = 0; |
338 | *aTrailingSpace = 0; |
339 | return true; |
340 | } |
341 | return false; |
342 | } |
343 | |
344 | // Ignore the combining "negation" suffix for 2-character strings. |
345 | // https://w3c.github.io/mathml-core/#dfn-algorithm-to-determine-the-category-of-an-operator |
346 | if (aOperator[1] == 0x0338 || aOperator[1] == 0x20D2) { |
347 | nsAutoString newOperator; |
348 | newOperator.Append(aOperator[0]); |
349 | return LookupOperator(newOperator, aForm, aFlags, aLeadingSpace, |
350 | aTrailingSpace); |
351 | } |
352 | } |
353 | |
354 | if (!gGlobalsInitialized) { |
355 | InitOperatorGlobals(); |
356 | } |
357 | if (gOperatorTable) { |
358 | if (OperatorData* data = GetOperatorData(aOperator, aForm)) { |
359 | NS_ASSERTION(data->mStr.Equals(aOperator), "bad setup")do { if (!(data->mStr.Equals(aOperator))) { NS_DebugBreak( NS_DEBUG_ASSERTION, "bad setup", "data->mStr.Equals(aOperator)" , "/var/lib/jenkins/workspace/firefox-scan-build/layout/mathml/nsMathMLOperators.cpp" , 359); MOZ_PretendNoReturn(); } } while (0); |
360 | *aFlags = data->mFlags; |
361 | *aLeadingSpace = data->mLeadingSpace; |
362 | *aTrailingSpace = data->mTrailingSpace; |
363 | return true; |
364 | } |
365 | } |
366 | |
367 | return false; |
368 | } |
369 | |
370 | bool nsMathMLOperators::LookupOperatorWithFallback(const nsString& aOperator, |
371 | const uint8_t aForm, |
372 | nsOperatorFlags* aFlags, |
373 | float* aLeadingSpace, |
374 | float* aTrailingSpace) { |
375 | if (LookupOperator(aOperator, aForm, aFlags, aLeadingSpace, aTrailingSpace)) { |
376 | return true; |
377 | } |
378 | for (const auto& form : |
379 | {NS_MATHML_OPERATOR_FORM_INFIX, NS_MATHML_OPERATOR_FORM_POSTFIX, |
380 | NS_MATHML_OPERATOR_FORM_PREFIX}) { |
381 | if (form == aForm) { |
382 | // This form was tried above, skip it. |
383 | continue; |
384 | } |
385 | if (LookupOperator(aOperator, form, aFlags, aLeadingSpace, |
386 | aTrailingSpace)) { |
387 | return true; |
388 | } |
389 | } |
390 | return false; |
391 | } |
392 | |
393 | /* static */ |
394 | bool nsMathMLOperators::IsMirrorableOperator(const nsString& aOperator) { |
395 | if (auto codePoint = ToUnicodeCodePoint(aOperator)) { |
396 | return mozilla::intl::UnicodeProperties::IsMirrored(codePoint); |
397 | } |
398 | return false; |
399 | } |
400 | |
401 | /* static */ |
402 | bool nsMathMLOperators::IsIntegralOperator(const nsString& aOperator) { |
403 | if (auto codePoint = ToUnicodeCodePoint(aOperator)) { |
404 | return (0x222B <= codePoint && codePoint <= 0x2233) || |
405 | (0x2A0B <= codePoint && codePoint <= 0x2A1C); |
406 | } |
407 | return false; |
408 | } |
409 | |
410 | /* static */ |
411 | nsStretchDirection nsMathMLOperators::GetStretchyDirection( |
412 | const nsString& aOperator) { |
413 | // Search any entry for that operator and return the corresponding direction. |
414 | // It is assumed that all the forms have same direction. |
415 | for (const auto& form : |
416 | {NS_MATHML_OPERATOR_FORM_INFIX, NS_MATHML_OPERATOR_FORM_POSTFIX, |
417 | NS_MATHML_OPERATOR_FORM_PREFIX}) { |
418 | nsOperatorFlags flags; |
419 | float dummy; |
420 | if (nsMathMLOperators::LookupOperator(aOperator, form, &flags, &dummy, |
421 | &dummy)) { |
422 | if (NS_MATHML_OPERATOR_IS_DIRECTION_VERTICAL(flags)(NS_MATHML_OPERATOR_DIRECTION_VERTICAL == ((flags) & NS_MATHML_OPERATOR_DIRECTION ))) { |
423 | return NS_STRETCH_DIRECTION_VERTICAL; |
424 | } |
425 | if (NS_MATHML_OPERATOR_IS_DIRECTION_HORIZONTAL(flags)(NS_MATHML_OPERATOR_DIRECTION_HORIZONTAL == ((flags) & NS_MATHML_OPERATOR_DIRECTION ))) { |
426 | return NS_STRETCH_DIRECTION_HORIZONTAL; |
427 | } |
428 | } |
429 | } |
430 | return NS_STRETCH_DIRECTION_UNSUPPORTED; |
431 | } |