Changeset 31953 in webkit
- Timestamp:
- Apr 16, 2008 12:19:57 PM (16 years ago)
- Location:
- trunk/WebCore
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/WebCore/ChangeLog
r31952 r31953 1 2008-04-15 Adam Roben <aroben@apple.com> 2 3 Refactor String.vsprintf to separate tokenizing and substituting 4 5 This will eventually be useful for the various console methods that 6 take a format string. 7 8 Reviewed by Tim Hatcher. 9 10 * page/inspector/utilities.js: 11 (String.tokenizeFormatString): Added. This uses the format parsing 12 code from String.vsprintf but just returns an array of "string" and 13 "specifier" tokens. 14 (String.vsprintf): Now calls String.tokenizeFormatString and processes 15 the tokens one at a time. 16 1 17 2008-04-15 Adam Roben <aroben@apple.com> 2 18 -
trunk/WebCore/page/inspector/utilities.js
r31889 r31953 753 753 } 754 754 755 String.vsprintf = function(format, substitutions) 756 { 757 if (!format || !substitutions || !substitutions.length) 758 return format; 759 760 var result = ""; 755 String.tokenizeFormatString = function(format) 756 { 757 var tokens = []; 761 758 var substitutionIndex = 0; 759 760 function addStringToken(str) 761 { 762 tokens.push({ type: "string", value: str }); 763 } 764 765 function addSpecifierToken(specifier, precision, substitutionIndex) 766 { 767 tokens.push({ type: "specifier", specifier: specifier, precision: precision, substitutionIndex: substitutionIndex }); 768 } 762 769 763 770 var index = 0; 764 771 for (var precentIndex = format.indexOf("%", index); precentIndex !== -1; precentIndex = format.indexOf("%", index)) { 765 result += format.substring(index, precentIndex);772 addStringToken(format.substring(index, precentIndex)); 766 773 index = precentIndex + 1; 767 774 768 775 if (format[index] === "%") { 769 result += "%";776 addStringToken("%"); 770 777 ++index; 771 778 continue; … … 797 804 } 798 805 799 if (substitutionIndex >= substitutions.length) { 806 addSpecifierToken(format[index], precision, substitutionIndex); 807 808 ++substitutionIndex; 809 ++index; 810 } 811 812 addStringToken(format.substring(index)); 813 814 return tokens; 815 } 816 817 String.vsprintf = function(format, substitutions) 818 { 819 if (!format || !substitutions || !substitutions.length) 820 return format; 821 822 function prettyFunctionName() 823 { 824 return "String.vsprintf(\"" + format + "\", \"" + substitutions.join("\", \"") + "\")"; 825 } 826 827 function warn(msg) 828 { 829 console.warn(prettyFunctionName() + ": " + msg); 830 } 831 832 function error(msg) 833 { 834 console.error(prettyFunctionName() + ": " + msg); 835 } 836 837 var result = ""; 838 var tokens = String.tokenizeFormatString(format); 839 840 for (var i = 0; i < tokens.length; ++i) { 841 var token = tokens[i]; 842 843 if (token.type === "string") { 844 result += token.value; 845 continue; 846 } 847 848 if (token.type !== "specifier") { 849 error("Unknown token type \"" + token.type + "\" found."); 850 continue; 851 } 852 853 if (token.substitutionIndex >= substitutions.length) { 800 854 // If there are not enough substitutions for the current substitutionIndex 801 855 // just output the format specifier literally and move on. 802 console.error("String.vsprintf(\"" + format + "\", \"" + substitutions.join("\", \"") + "\"): not enough substitution arguments. Had " + substitutions.length + " but needed " + (substitutionIndex + 1) + ", so substitution was skipped."); 803 index = precentIndex + 1; 804 result += "%"; 856 error("not enough substitution arguments. Had " + substitutions.length + " but needed " + (token.substitutionIndex + 1) + ", so substitution was skipped."); 857 result += "%" + (token.precision > -1 ? token.precision : "") + token.specifier; 805 858 continue; 806 859 } 807 860 808 switch ( format[index]) {861 switch (token.specifier) { 809 862 case "d": 810 var substitution = parseInt(substitutions[ substitutionIndex]);863 var substitution = parseInt(substitutions[token.substitutionIndex]); 811 864 result += (!isNaN(substitution) ? substitution : 0); 812 865 break; 813 866 case "f": 814 var substitution = parseFloat(substitutions[ substitutionIndex]);815 if (substitution && precision > -1)816 substitution = substitution.toFixed( precision);817 result += (!isNaN(substitution) ? substitution : ( precision > -1 ? Number(0).toFixed(precision) : 0));867 var substitution = parseFloat(substitutions[token.substitutionIndex]); 868 if (substitution && token.precision > -1) 869 substitution = substitution.toFixed(token.precision); 870 result += (!isNaN(substitution) ? substitution : (token.precision > -1 ? Number(0).toFixed(token.precision) : 0)); 818 871 break; 819 872 default: 820 873 // Encountered an unsupported format character, treat as a string. 821 console.warn("String.vsprintf(\"" + format + "\", \"" + substitutions.join("\", \"") + "\"): unsupported format character \u201C" + format[index]+ "\u201D. Treating as a string.");874 warn("unsupported format character \u201C" + token.specifier + "\u201D. Treating as a string."); 822 875 // Fall through to treat this like a string. 823 876 case "s": 824 result += substitutions[substitutionIndex]; 825 break; 826 } 827 828 ++substitutionIndex; 829 ++index; 830 } 831 832 result += format.substring(index); 877 result += substitutions[token.substitutionIndex]; 878 break; 879 } 880 } 833 881 834 882 return result;
Note: See TracChangeset
for help on using the changeset viewer.