OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 28 matching lines...) Expand all Loading... | |
39 #include "wtf/text/WTFString.h" | 39 #include "wtf/text/WTFString.h" |
40 | 40 |
41 namespace WebCore { | 41 namespace WebCore { |
42 | 42 |
43 const double secondsPerHour = 3600; | 43 const double secondsPerHour = 3600; |
44 const double secondsPerMinute = 60; | 44 const double secondsPerMinute = 60; |
45 const double secondsPerMillisecond = 0.001; | 45 const double secondsPerMillisecond = 0.001; |
46 const double malformedTime = -1; | 46 const double malformedTime = -1; |
47 const unsigned fileIdentifierLength = 6; | 47 const unsigned fileIdentifierLength = 6; |
48 | 48 |
49 String VTTParser::collectDigits(const String& input, unsigned* position) | 49 static unsigned scanDigits(const String& input, unsigned* position) |
50 { | 50 { |
51 StringBuilder digits; | 51 unsigned startPosition = *position; |
52 while (*position < input.length() && isASCIIDigit(input[*position])) | 52 while (*position < input.length() && isASCIIDigit(input[*position])) |
53 digits.append(input[(*position)++]); | 53 (*position)++; |
54 return digits.toString(); | 54 return *position - startPosition; |
55 } | 55 } |
56 | 56 |
57 unsigned VTTParser::collectDigitsToInt(const String& input, unsigned* position, int& number) | 57 unsigned VTTParser::collectDigitsToInt(const String& input, unsigned* position, int& number) |
58 { | 58 { |
59 String digits = collectDigits(input, position); | 59 unsigned startPosition = *position; |
60 unsigned numDigits = scanDigits(input, position); | |
61 if (!numDigits) { | |
62 number = 0; | |
63 return 0; | |
64 } | |
60 bool validNumber; | 65 bool validNumber; |
61 number = digits.toInt(&validNumber); | 66 if (input.is8Bit()) |
62 // Since we know that |digits| only contain valid (ASCII) digits | 67 number = charactersToInt(input.characters8() + startPosition, numDigits, &validNumber); |
63 // (disregarding the 'empty' case), the remaining failure mode for toInt() | 68 else |
64 // is overflow, so if |validNumber| is not true, then set |number| to the | 69 number = charactersToInt(input.characters16() + startPosition, numDigits , &validNumber); |
65 // maximum int value. | 70 |
66 if (!digits.isEmpty() && !validNumber) | 71 // Since we know that scanDigits only scanned valid (ASCII) digits (and |
72 // hence that's what got passed to charactersToInt()), the remaining | |
73 // failure mode for charactersToInt() is overflow, so if |validNumber| is | |
74 // not true, then set |number| to the maximum int value. | |
75 if (!validNumber) | |
67 number = std::numeric_limits<int>::max(); | 76 number = std::numeric_limits<int>::max(); |
68 return digits.length(); | 77 return numDigits; |
69 } | 78 } |
70 | 79 |
71 String VTTParser::collectWord(const String& input, unsigned* position) | 80 String VTTParser::collectWord(const String& input, unsigned* position) |
72 { | 81 { |
73 StringBuilder string; | 82 StringBuilder string; |
74 while (*position < input.length() && !isASpace(input[*position])) | 83 while (*position < input.length() && !isASpace(input[*position])) |
75 string.append(input[(*position)++]); | 84 string.append(input[(*position)++]); |
76 return string.toString(); | 85 return string.toString(); |
77 } | 86 } |
78 | 87 |
79 void VTTParser::skipWhiteSpace(const String& line, unsigned* position) | 88 void VTTParser::skipWhiteSpace(const String& line, unsigned* position) |
80 { | 89 { |
81 while (*position < line.length() && isASpace(line[*position])) | 90 while (*position < line.length() && isASpace(line[*position])) |
82 (*position)++; | 91 (*position)++; |
83 } | 92 } |
84 | 93 |
85 float VTTParser::parseFloatPercentageValue(const String& value, bool& isValidSet ting) | 94 float VTTParser::parseFloatPercentageValue(const String& value, bool& isValidSet ting) |
86 { | 95 { |
87 // '%' must be present and at the end of the setting value. | 96 // '%' must be present and at the end of the setting value. |
88 if (value.find('%', 1) != value.length() - 1) { | 97 if (value.find('%', 1) != value.length() - 1) { |
89 isValidSetting = false; | 98 isValidSetting = false; |
90 return 0; | 99 return 0; |
91 } | 100 } |
92 | 101 |
93 unsigned position = 0; | 102 unsigned position = 0; |
94 | 103 unsigned digitsBeforeDot = scanDigits(value, &position); |
95 StringBuilder floatNumberAsString; | 104 unsigned digitsAfterDot; |
Mike West
2013/12/04 08:17:43
If you default this to 0, you can drop the 'else'
| |
96 floatNumberAsString.append(VTTParser::collectDigits(value, &position)); | |
97 | |
98 if (value[position] == '.') { | 105 if (value[position] == '.') { |
99 floatNumberAsString.append("."); | |
100 position++; | 106 position++; |
101 | 107 |
102 floatNumberAsString.append(VTTParser::collectDigits(value, &position)); | 108 digitsAfterDot = scanDigits(value, &position); |
109 } else { | |
110 digitsAfterDot = 0; | |
103 } | 111 } |
104 float number = floatNumberAsString.toString().toFloat(&isValidSetting); | |
105 | 112 |
106 if (isValidSetting && (number <= 0 || number >= 100)) | 113 // At least one digit required. |
114 if (!digitsBeforeDot && !digitsAfterDot) { | |
107 isValidSetting = false; | 115 isValidSetting = false; |
116 return 0; | |
117 } | |
108 | 118 |
119 float number = value.toFloat(); | |
120 isValidSetting = number >= 0 && number <= 100; | |
109 return number; | 121 return number; |
110 } | 122 } |
111 | 123 |
112 FloatPoint VTTParser::parseFloatPercentageValuePair(const String& value, char de limiter, bool& isValidSetting) | 124 FloatPoint VTTParser::parseFloatPercentageValuePair(const String& value, char de limiter, bool& isValidSetting) |
113 { | 125 { |
114 // The delimiter can't be the first or second value because a pair of | 126 // The delimiter can't be the first or second value because a pair of |
115 // percentages (x%,y%) implies that at least the first two characters | 127 // percentages (x%,y%) implies that at least the first two characters |
116 // are the first percentage value. | 128 // are the first percentage value. |
117 size_t delimiterOffset = value.find(delimiter, 2); | 129 size_t delimiterOffset = value.find(delimiter, 2); |
118 if (delimiterOffset == kNotFound || delimiterOffset == value.length() - 1) { | 130 if (delimiterOffset == kNotFound || delimiterOffset == value.length() - 1) { |
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
616 m_currentNode->parserAppendChild(ProcessingInstruction::create(docum ent, "timestamp", charactersString)); | 628 m_currentNode->parserAppendChild(ProcessingInstruction::create(docum ent, "timestamp", charactersString)); |
617 break; | 629 break; |
618 } | 630 } |
619 default: | 631 default: |
620 break; | 632 break; |
621 } | 633 } |
622 } | 634 } |
623 | 635 |
624 } | 636 } |
625 | 637 |
OLD | NEW |