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

Side by Side Diff: Source/core/html/track/vtt/VTTParser.cpp

Issue 101513002: Add new helper VTTParser::collectDigitsToInt (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Rebased Created 7 years 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
OLDNEW
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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 const unsigned fileIdentifierLength = 6; 47 const unsigned fileIdentifierLength = 6;
48 48
49 String VTTParser::collectDigits(const String& input, unsigned* position) 49 String VTTParser::collectDigits(const String& input, unsigned* position)
50 { 50 {
51 StringBuilder digits; 51 StringBuilder digits;
52 while (*position < input.length() && isASCIIDigit(input[*position])) 52 while (*position < input.length() && isASCIIDigit(input[*position]))
53 digits.append(input[(*position)++]); 53 digits.append(input[(*position)++]);
54 return digits.toString(); 54 return digits.toString();
55 } 55 }
56 56
57 unsigned VTTParser::collectDigitsToInt(const String& input, unsigned* position, int& number)
58 {
59 String digits = collectDigits(input, position);
60 bool validNumber;
61 number = digits.toInt(&validNumber);
62 // Since we know that |digits| only contain valid (ASCII) digits
63 // (disregarding the 'empty' case), the remaining failure mode for toInt()
64 // is overflow, so if |validNumber| is not true, then set |number| to the
65 // maximum int value.
66 if (!digits.isEmpty() && !validNumber)
67 number = std::numeric_limits<int>::max();
68 return digits.length();
69 }
70
57 String VTTParser::collectWord(const String& input, unsigned* position) 71 String VTTParser::collectWord(const String& input, unsigned* position)
58 { 72 {
59 StringBuilder string; 73 StringBuilder string;
60 while (*position < input.length() && !isASpace(input[*position])) 74 while (*position < input.length() && !isASpace(input[*position]))
61 string.append(input[(*position)++]); 75 string.append(input[(*position)++]);
62 return string.toString(); 76 return string.toString();
63 } 77 }
64 78
79 void VTTParser::skipWhiteSpace(const String& line, unsigned* position)
80 {
81 while (*position < line.length() && isASpace(line[*position]))
82 (*position)++;
83 }
84
65 float VTTParser::parseFloatPercentageValue(const String& value, bool& isValidSet ting) 85 float VTTParser::parseFloatPercentageValue(const String& value, bool& isValidSet ting)
66 { 86 {
67 // '%' must be present and at the end of the setting value. 87 // '%' must be present and at the end of the setting value.
68 if (value.find('%', 1) != value.length() - 1) { 88 if (value.find('%', 1) != value.length() - 1) {
69 isValidSetting = false; 89 isValidSetting = false;
70 return 0; 90 return 0;
71 } 91 }
72 92
73 unsigned position = 0; 93 unsigned position = 0;
74 94
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 464
445 double VTTParser::collectTimeStamp(const String& line, unsigned* position) 465 double VTTParser::collectTimeStamp(const String& line, unsigned* position)
446 { 466 {
447 // Collect a WebVTT timestamp (5.3 WebVTT cue timings and settings parsing.) 467 // Collect a WebVTT timestamp (5.3 WebVTT cue timings and settings parsing.)
448 // Steps 1 - 4 - Initial checks, let most significant units be minutes. 468 // Steps 1 - 4 - Initial checks, let most significant units be minutes.
449 enum Mode { Minutes, Hours }; 469 enum Mode { Minutes, Hours };
450 Mode mode = Minutes; 470 Mode mode = Minutes;
451 if (*position >= line.length() || !isASCIIDigit(line[*position])) 471 if (*position >= line.length() || !isASCIIDigit(line[*position]))
452 return malformedTime; 472 return malformedTime;
453 473
454 // Steps 5 - 6 - Collect a sequence of characters that are 0-9. 474 int value1;
455 String digits1 = collectDigits(line, position); 475 // Steps 5 - 7 - Collect a sequence of characters that are 0-9.
456 int value1 = digits1.toInt(); 476 // If not 2 characters or value is greater than 59, interpret as hours.
457 477 if (collectDigitsToInt(line, position, value1) != 2 || value1 > 59)
458 // Step 7 - If not 2 characters or value is greater than 59, interpret as ho urs.
459 if (digits1.length() != 2 || value1 > 59)
460 mode = Hours; 478 mode = Hours;
461 479
462 // Steps 8 - 11 - Collect the next sequence of 0-9 after ':' (must be 2 char s). 480 // Steps 8 - 11 - Collect the next sequence of 0-9 after ':' (must be 2 char s).
463 if (*position >= line.length() || line[(*position)++] != ':') 481 if (*position >= line.length() || line[(*position)++] != ':')
464 return malformedTime; 482 return malformedTime;
465 if (*position >= line.length() || !isASCIIDigit(line[(*position)])) 483 if (*position >= line.length() || !isASCIIDigit(line[(*position)]))
466 return malformedTime; 484 return malformedTime;
467 String digits2 = collectDigits(line, position); 485 int value2;
468 int value2 = digits2.toInt(); 486 if (collectDigitsToInt(line, position, value2) != 2)
469 if (digits2.length() != 2)
470 return malformedTime; 487 return malformedTime;
471 488
472 // Step 12 - Detect whether this timestamp includes hours. 489 // Step 12 - Detect whether this timestamp includes hours.
473 int value3; 490 int value3;
474 if (mode == Hours || (*position < line.length() && line[*position] == ':')) { 491 if (mode == Hours || (*position < line.length() && line[*position] == ':')) {
475 if (*position >= line.length() || line[(*position)++] != ':') 492 if (*position >= line.length() || line[(*position)++] != ':')
476 return malformedTime; 493 return malformedTime;
477 if (*position >= line.length() || !isASCIIDigit(line[*position])) 494 if (*position >= line.length() || !isASCIIDigit(line[*position]))
478 return malformedTime; 495 return malformedTime;
479 String digits3 = collectDigits(line, position); 496 if (collectDigitsToInt(line, position, value3) != 2)
480 if (digits3.length() != 2)
481 return malformedTime; 497 return malformedTime;
482 value3 = digits3.toInt();
483 } else { 498 } else {
484 value3 = value2; 499 value3 = value2;
485 value2 = value1; 500 value2 = value1;
486 value1 = 0; 501 value1 = 0;
487 } 502 }
488 503
489 // Steps 13 - 17 - Collect next sequence of 0-9 after '.' (must be 3 chars). 504 // Steps 13 - 17 - Collect next sequence of 0-9 after '.' (must be 3 chars).
490 if (*position >= line.length() || line[(*position)++] != '.') 505 if (*position >= line.length() || line[(*position)++] != '.')
491 return malformedTime; 506 return malformedTime;
492 if (*position >= line.length() || !isASCIIDigit(line[*position])) 507 if (*position >= line.length() || !isASCIIDigit(line[*position]))
493 return malformedTime; 508 return malformedTime;
494 String digits4 = collectDigits(line, position); 509 int value4;
495 if (digits4.length() != 3) 510 if (collectDigitsToInt(line, position, value4) != 3)
496 return malformedTime; 511 return malformedTime;
497 int value4 = digits4.toInt();
498 if (value2 > 59 || value3 > 59) 512 if (value2 > 59 || value3 > 59)
499 return malformedTime; 513 return malformedTime;
500 514
501 // Steps 18 - 19 - Calculate result. 515 // Steps 18 - 19 - Calculate result.
502 return (value1 * secondsPerHour) + (value2 * secondsPerMinute) + value3 + (v alue4 * secondsPerMillisecond); 516 return (value1 * secondsPerHour) + (value2 * secondsPerMinute) + value3 + (v alue4 * secondsPerMillisecond);
503 } 517 }
504 518
505 static VTTNodeType tokenToNodeType(VTTToken& token) 519 static VTTNodeType tokenToNodeType(VTTToken& token)
506 { 520 {
507 switch (token.name().length()) { 521 switch (token.name().length()) {
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
600 double time = VTTParser::collectTimeStamp(charactersString, &position); 614 double time = VTTParser::collectTimeStamp(charactersString, &position);
601 if (time != malformedTime) 615 if (time != malformedTime)
602 m_currentNode->parserAppendChild(ProcessingInstruction::create(docum ent, "timestamp", charactersString)); 616 m_currentNode->parserAppendChild(ProcessingInstruction::create(docum ent, "timestamp", charactersString));
603 break; 617 break;
604 } 618 }
605 default: 619 default:
606 break; 620 break;
607 } 621 }
608 } 622 }
609 623
610 void VTTParser::skipWhiteSpace(const String& line, unsigned* position)
611 {
612 while (*position < line.length() && isASpace(line[*position]))
613 (*position)++;
614 }
615
616 } 624 }
617 625
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698