Update wmts-hidpi, add nicer-api-docs

This commit is contained in:
Andreas Hocevar
2014-05-06 13:02:46 -05:00
parent b3ac1afd00
commit 1e25fc5585
2239 changed files with 3726515 additions and 37010 deletions

View File

@@ -0,0 +1,236 @@
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Utility function for linkifying text.
* @author bolinfest@google.com (Michael Bolin)
*/
goog.provide('goog.string.linkify');
goog.require('goog.string');
/**
* Takes a string of plain text and linkifies URLs and email addresses. For a
* URL (unless opt_attributes is specified), the target of the link will be
* _blank and it will have a rel=nofollow attribute applied to it so that links
* created by linkify will not be of interest to search engines.
* @param {string} text Plain text.
* @param {Object.<string, string>=} opt_attributes Attributes to add to all
* links created. Default are rel=nofollow and target=blank. To clear those
* default attributes set rel='' and target='_blank'.
* @return {string} HTML Linkified HTML text.
*/
goog.string.linkify.linkifyPlainText = function(text, opt_attributes) {
var attributesMap = opt_attributes || {};
// Set default options.
if (!('rel' in attributesMap)) {
attributesMap['rel'] = 'nofollow';
}
if (!('target' in attributesMap)) {
attributesMap['target'] = '_blank';
}
// Creates attributes string from options.
var attributesArray = [];
for (var key in attributesMap) {
if (attributesMap.hasOwnProperty(key) && attributesMap[key]) {
attributesArray.push(
goog.string.htmlEscape(key), '="',
goog.string.htmlEscape(attributesMap[key]), '" ');
}
}
var attributes = attributesArray.join('');
return text.replace(
goog.string.linkify.FIND_LINKS_RE_,
function(part, before, original, email, protocol) {
var output = [goog.string.htmlEscape(before)];
if (!original) {
return output[0];
}
output.push('<a ', attributes, 'href="');
/** @type {string} */
var linkText;
/** @type {string} */
var afterLink;
if (email) {
output.push('mailto:');
linkText = email;
afterLink = '';
} else {
// This is a full url link.
if (!protocol) {
output.push('http://');
}
var splitEndingPunctuation =
original.match(goog.string.linkify.ENDS_WITH_PUNCTUATION_RE_);
if (splitEndingPunctuation) {
linkText = splitEndingPunctuation[1];
afterLink = splitEndingPunctuation[2];
} else {
linkText = original;
afterLink = '';
}
}
linkText = goog.string.htmlEscape(linkText);
afterLink = goog.string.htmlEscape(afterLink);
output.push(linkText, '">', linkText, '</a>', afterLink);
return output.join('');
});
};
/**
* Gets the first URI in text.
* @param {string} text Plain text.
* @return {string} The first URL, or an empty string if not found.
*/
goog.string.linkify.findFirstUrl = function(text) {
var link = text.match(goog.string.linkify.URL_);
return link != null ? link[0] : '';
};
/**
* Gets the first email address in text.
* @param {string} text Plain text.
* @return {string} The first email address, or an empty string if not found.
*/
goog.string.linkify.findFirstEmail = function(text) {
var email = text.match(goog.string.linkify.EMAIL_);
return email != null ? email[0] : '';
};
/**
* If a series of these characters is at the end of a url, it will be considered
* punctuation and not part of the url.
* @type {string}
* @const
* @private
*/
goog.string.linkify.ENDING_PUNCTUATION_CHARS_ = ':;,\\.?>\\]\\)!';
/**
* @type {!RegExp}
* @const
* @private
*/
goog.string.linkify.ENDS_WITH_PUNCTUATION_RE_ = new RegExp(
'^(.*?)([' + goog.string.linkify.ENDING_PUNCTUATION_CHARS_ + ']+)$');
/**
* Set of characters to be put into a regex character set ("[...]"), used to
* match against a url hostname and everything after it. It includes
* "#-@", which represents the characters "#$%&'()*+,-./0123456789:;<=>?@".
* @type {string}
* @const
* @private
*/
goog.string.linkify.ACCEPTABLE_URL_CHARS_ = '\\w~#-@!\\[\\]';
/**
* List of all protocols patterns recognized in urls (mailto is handled in email
* matching).
* @type {!Array.<string>}
* @const
* @private
*/
goog.string.linkify.RECOGNIZED_PROTOCOLS_ = ['https?', 'ftp'];
/**
* Regular expression pattern that matches the beginning of an url.
* Contains a catching group to capture the scheme.
* @type {string}
* @const
* @private
*/
goog.string.linkify.PROTOCOL_START_ =
'(' + goog.string.linkify.RECOGNIZED_PROTOCOLS_.join('|') + ')://';
/**
* Regular expression pattern that matches the beginning of a typical
* http url without the http:// scheme.
* @type {string}
* @const
* @private
*/
goog.string.linkify.WWW_START_ = 'www\\.';
/**
* Regular expression pattern that matches an url.
* @type {string}
* @const
* @private
*/
goog.string.linkify.URL_ =
'(?:' + goog.string.linkify.PROTOCOL_START_ + '|' +
goog.string.linkify.WWW_START_ + ')\\w[' +
goog.string.linkify.ACCEPTABLE_URL_CHARS_ + ']*';
/**
* Regular expression pattern that matches a top level domain.
* @type {string}
* @const
* @private
*/
goog.string.linkify.TOP_LEVEL_DOMAIN_ =
'(?:com|org|net|edu|gov' +
// from http://www.iana.org/gtld/gtld.htm
'|aero|biz|cat|coop|info|int|jobs|mobi|museum|name|pro|travel' +
'|arpa|asia|xxx' +
// a two letter country code
'|[a-z][a-z])\\b';
/**
* Regular expression pattern that matches an email.
* Contains a catching group to capture the email without the optional "mailto:"
* prefix.
* @type {string}
* @const
* @private
*/
goog.string.linkify.EMAIL_ =
'(?:mailto:)?([\\w.+-]+@[A-Za-z0-9.-]+\\.' +
goog.string.linkify.TOP_LEVEL_DOMAIN_ + ')';
/**
* Regular expression to match all the links (url or email) in a string.
* First match is text before first link, might be empty string.
* Second match is the original text that should be replaced by a link.
* Third match is the email address in the case of an email.
* Fourth match is the scheme of the url if specified.
* @type {!RegExp}
* @const
* @private
*/
goog.string.linkify.FIND_LINKS_RE_ = new RegExp(
// Match everything including newlines.
'([\\S\\s]*?)(' +
// Match email after a word break.
'\\b' + goog.string.linkify.EMAIL_ + '|' +
// Match url after a workd break.
'\\b' + goog.string.linkify.URL_ + '|$)',
'g');

View File

@@ -0,0 +1,153 @@
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Utilities for string newlines.
* @author nnaze@google.com (Nathan Naze)
*/
/**
* Namespace for string utilities
*/
goog.provide('goog.string.newlines');
goog.provide('goog.string.newlines.Line');
goog.require('goog.array');
/**
* Splits a string into lines, properly handling universal newlines.
* @param {string} str String to split.
* @param {boolean=} opt_keepNewlines Whether to keep the newlines in the
* resulting strings. Defaults to false.
* @return {!Array.<string>} String split into lines.
*/
goog.string.newlines.splitLines = function(str, opt_keepNewlines) {
var lines = goog.string.newlines.getLines(str);
return goog.array.map(lines, function(line) {
return opt_keepNewlines ? line.getFullLine() : line.getContent();
});
};
/**
* Line metadata class that records the start/end indicies of lines
* in a string. Can be used to implement common newline use cases such as
* splitLines() or determining line/column of an index in a string.
* Also implements methods to get line contents.
*
* Indexes are expressed as string indicies into string.substring(), inclusive
* at the start, exclusive at the end.
*
* Create an array of these with goog.string.newlines.getLines().
* @param {string} string The original string.
* @param {number} startLineIndex The index of the start of the line.
* @param {number} endContentIndex The index of the end of the line, excluding
* newlines.
* @param {number} endLineIndex The index of the end of the line, index
* newlines.
* @constructor
* @struct
*/
goog.string.newlines.Line = function(string, startLineIndex,
endContentIndex, endLineIndex) {
/**
* The original string.
* @type {string}
*/
this.string = string;
/**
* Index of the start of the line.
* @type {number}
*/
this.startLineIndex = startLineIndex;
/**
* Index of the end of the line, excluding any newline characters.
* Index is the first character after the line, suitable for
* String.substring().
* @type {number}
*/
this.endContentIndex = endContentIndex;
/**
* Index of the end of the line, excluding any newline characters.
* Index is the first character after the line, suitable for
* String.substring().
* @type {number}
*/
this.endLineIndex = endLineIndex;
};
/**
* @return {string} The content of the line, excluding any newline characters.
*/
goog.string.newlines.Line.prototype.getContent = function() {
return this.string.substring(this.startLineIndex, this.endContentIndex);
};
/**
* @return {string} The full line, including any newline characters.
*/
goog.string.newlines.Line.prototype.getFullLine = function() {
return this.string.substring(this.startLineIndex, this.endLineIndex);
};
/**
* @return {string} The newline characters, if any ('\n', \r', '\r\n', '', etc).
*/
goog.string.newlines.Line.prototype.getNewline = function() {
return this.string.substring(this.endContentIndex, this.endLineIndex);
};
/**
* Splits a string into an array of line metadata.
* @param {string} str String to split.
* @return {!Array.<!goog.string.newlines.Line>} Array of line metadata.
*/
goog.string.newlines.getLines = function(str) {
// We use the constructor because literals are evaluated only once in
// < ES 3.1.
// See http://www.mail-archive.com/es-discuss@mozilla.org/msg01796.html
var re = RegExp('\r\n|\r|\n', 'g');
var sliceIndex = 0;
var result;
var lines = [];
while (result = re.exec(str)) {
var line = new goog.string.newlines.Line(
str, sliceIndex, result.index, result.index + result[0].length);
lines.push(line);
// remember where to start the slice from
sliceIndex = re.lastIndex;
}
// If the string does not end with a newline, add the last line.
if (sliceIndex < str.length) {
var line = new goog.string.newlines.Line(
str, sliceIndex, str.length, str.length);
lines.push(line);
}
return lines;
};

View File

@@ -0,0 +1,87 @@
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Unit tests for goog.string.
*/
goog.provide('goog.string.newlinesTest');
goog.require('goog.string.newlines');
goog.require('goog.testing.jsunit');
goog.setTestOnly('goog.string.newlinesTest');
// test for goog.string.splitLines
function testSplitLines() {
/**
* @param {!Array.<string>} expected
* @param {string} string
* @param {boolean=} opt_keepNewlines
*/
function assertSplitLines(expected, string, opt_keepNewlines) {
var keepNewlines = opt_keepNewlines || false;
var lines = goog.string.newlines.splitLines(string, keepNewlines);
assertElementsEquals(expected, lines);
}
// Test values borrowed from Python's splitlines. http://goo.gl/iwawx
assertSplitLines(['abc', 'def', '', 'ghi'], 'abc\ndef\n\rghi');
assertSplitLines(['abc', 'def', '', 'ghi'], 'abc\ndef\n\r\nghi');
assertSplitLines(['abc', 'def', 'ghi'], 'abc\ndef\r\nghi');
assertSplitLines(['abc', 'def', 'ghi'], 'abc\ndef\r\nghi\n');
assertSplitLines(['abc', 'def', 'ghi', ''], 'abc\ndef\r\nghi\n\r');
assertSplitLines(['', 'abc', 'def', 'ghi', ''], '\nabc\ndef\r\nghi\n\r');
assertSplitLines(['', 'abc', 'def', 'ghi', ''], '\nabc\ndef\r\nghi\n\r');
assertSplitLines(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'],
'\nabc\ndef\r\nghi\n\r', true);
assertSplitLines(['', 'abc', 'def', 'ghi', ''], '\nabc\ndef\r\nghi\n\r');
assertSplitLines(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'],
'\nabc\ndef\r\nghi\n\r', true);
}
function testGetLines() {
var lines = goog.string.newlines.getLines('abc\ndef\n\rghi');
assertEquals(4, lines.length);
assertEquals(0, lines[0].startLineIndex);
assertEquals(3, lines[0].endContentIndex);
assertEquals(4, lines[0].endLineIndex);
assertEquals('abc', lines[0].getContent());
assertEquals('abc\n', lines[0].getFullLine());
assertEquals('\n', lines[0].getNewline());
assertEquals(4, lines[1].startLineIndex);
assertEquals(7, lines[1].endContentIndex);
assertEquals(8, lines[1].endLineIndex);
assertEquals('def', lines[1].getContent());
assertEquals('def\n', lines[1].getFullLine());
assertEquals('\n', lines[1].getNewline());
assertEquals(8, lines[2].startLineIndex);
assertEquals(8, lines[2].endContentIndex);
assertEquals(9, lines[2].endLineIndex);
assertEquals('', lines[2].getContent());
assertEquals('\r', lines[2].getFullLine());
assertEquals('\r', lines[2].getNewline());
assertEquals(9, lines[3].startLineIndex);
assertEquals(12, lines[3].endContentIndex);
assertEquals(12, lines[3].endLineIndex);
assertEquals('ghi', lines[3].getContent());
assertEquals('ghi', lines[3].getFullLine());
assertEquals('', lines[3].getNewline());
}

View File

@@ -0,0 +1,38 @@
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Defines an interface for parsing strings into objects.
*/
goog.provide('goog.string.Parser');
/**
* An interface for parsing strings into objects.
* @interface
*/
goog.string.Parser = function() {};
/**
* Parses a string into an object and returns the result.
* Agnostic to the format of string and object.
*
* @param {string} s The string to parse.
* @return {*} The object generated from the string.
*/
goog.string.Parser.prototype.parse;

View File

@@ -0,0 +1,169 @@
// Copyright 2010 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Utilities for dealing with POSIX path strings. Based on
* Python's os.path and posixpath.
* @author nnaze@google.com (Nathan Naze)
*/
goog.provide('goog.string.path');
goog.require('goog.array');
goog.require('goog.string');
/**
* Returns the final component of a pathname.
* See http://docs.python.org/library/os.path.html#os.path.basename
* @param {string} path A pathname.
* @return {string} path The final component of a pathname, i.e. everything
* after the final slash.
*/
goog.string.path.baseName = function(path) {
var i = path.lastIndexOf('/') + 1;
return path.slice(i);
};
/**
* Alias to goog.string.path.baseName.
* @param {string} path A pathname.
* @return {string} path The final component of a pathname.
* @deprecated Use goog.string.path.baseName.
*/
goog.string.path.basename = goog.string.path.baseName;
/**
* Returns the directory component of a pathname.
* See http://docs.python.org/library/os.path.html#os.path.dirname
* @param {string} path A pathname.
* @return {string} The directory component of a pathname, i.e. everything
* leading up to the final slash.
*/
goog.string.path.dirname = function(path) {
var i = path.lastIndexOf('/') + 1;
var head = path.slice(0, i);
// If the path isn't all forward slashes, trim the trailing slashes.
if (!/^\/+$/.test(head)) {
head = head.replace(/\/+$/, '');
}
return head;
};
/**
* Extracts the extension part of a pathname.
* @param {string} path The path name to process.
* @return {string} The extension if any, otherwise the empty string.
*/
goog.string.path.extension = function(path) {
var separator = '.';
// Combining all adjacent periods in the basename to a single period.
var baseName = goog.string.path.baseName(path).replace(/\.+/g, separator);
var separatorIndex = baseName.lastIndexOf(separator);
return separatorIndex <= 0 ? '' : baseName.substr(separatorIndex + 1);
};
/**
* Joins one or more path components (e.g. 'foo/' and 'bar' make 'foo/bar').
* An absolute component will discard all previous component.
* See http://docs.python.org/library/os.path.html#os.path.join
* @param {...string} var_args One of more path components.
* @return {string} The path components joined.
*/
goog.string.path.join = function(var_args) {
var path = arguments[0];
for (var i = 1; i < arguments.length; i++) {
var arg = arguments[i];
if (goog.string.startsWith(arg, '/')) {
path = arg;
} else if (path == '' || goog.string.endsWith(path, '/')) {
path += arg;
} else {
path += '/' + arg;
}
}
return path;
};
/**
* Normalizes a pathname by collapsing duplicate separators, parent directory
* references ('..'), and current directory references ('.').
* See http://docs.python.org/library/os.path.html#os.path.normpath
* @param {string} path One or more path components.
* @return {string} The path after normalization.
*/
goog.string.path.normalizePath = function(path) {
if (path == '') {
return '.';
}
var initialSlashes = '';
// POSIX will keep two slashes, but three or more will be collapsed to one.
if (goog.string.startsWith(path, '/')) {
initialSlashes = '/';
if (goog.string.startsWith(path, '//') &&
!goog.string.startsWith(path, '///')) {
initialSlashes = '//';
}
}
var parts = path.split('/');
var newParts = [];
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
// '' and '.' don't change the directory, ignore.
if (part == '' || part == '.') {
continue;
}
// A '..' should pop a directory unless this is not an absolute path and
// we're at the root, or we've travelled upwards relatively in the last
// iteration.
if (part != '..' ||
(!initialSlashes && !newParts.length) ||
goog.array.peek(newParts) == '..') {
newParts.push(part);
} else {
newParts.pop();
}
}
var returnPath = initialSlashes + newParts.join('/');
return returnPath || '.';
};
/**
* Splits a pathname into "dirname" and "baseName" components, where "baseName"
* is everything after the final slash. Either part may return an empty string.
* See http://docs.python.org/library/os.path.html#os.path.split
* @param {string} path A pathname.
* @return {!Array.<string>} An array of [dirname, basename].
*/
goog.string.path.split = function(path) {
var head = goog.string.path.dirname(path);
var tail = goog.string.path.baseName(path);
return [head, tail];
};
// TODO(nnaze): Implement other useful functions from os.path

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,103 @@
// Copyright 2006 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Utility for fast string concatenation.
*/
goog.provide('goog.string.StringBuffer');
/**
* Utility class to facilitate string concatenation.
*
* @param {*=} opt_a1 Optional first initial item to append.
* @param {...*} var_args Other initial items to
* append, e.g., new goog.string.StringBuffer('foo', 'bar').
* @constructor
*/
goog.string.StringBuffer = function(opt_a1, var_args) {
if (opt_a1 != null) {
this.append.apply(this, arguments);
}
};
/**
* Internal buffer for the string to be concatenated.
* @type {string}
* @private
*/
goog.string.StringBuffer.prototype.buffer_ = '';
/**
* Sets the contents of the string buffer object, replacing what's currently
* there.
*
* @param {*} s String to set.
*/
goog.string.StringBuffer.prototype.set = function(s) {
this.buffer_ = '' + s;
};
/**
* Appends one or more items to the buffer.
*
* Calling this with null, undefined, or empty arguments is an error.
*
* @param {*} a1 Required first string.
* @param {*=} opt_a2 Optional second string.
* @param {...*} var_args Other items to append,
* e.g., sb.append('foo', 'bar', 'baz').
* @return {goog.string.StringBuffer} This same StringBuffer object.
* @suppress {duplicate}
*/
goog.string.StringBuffer.prototype.append = function(a1, opt_a2, var_args) {
// Use a1 directly to avoid arguments instantiation for single-arg case.
this.buffer_ += a1;
if (opt_a2 != null) { // second argument is undefined (null == undefined)
for (var i = 1; i < arguments.length; i++) {
this.buffer_ += arguments[i];
}
}
return this;
};
/**
* Clears the internal buffer.
*/
goog.string.StringBuffer.prototype.clear = function() {
this.buffer_ = '';
};
/**
* @return {number} the length of the current contents of the buffer.
*/
goog.string.StringBuffer.prototype.getLength = function() {
return this.buffer_.length;
};
/**
* @return {string} The concatenated string.
* @override
*/
goog.string.StringBuffer.prototype.toString = function() {
return this.buffer_;
};

View File

@@ -0,0 +1,251 @@
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Implementation of sprintf-like, python-%-operator-like,
* .NET-String.Format-like functionality. Uses JS string's replace method to
* extract format specifiers and sends those specifiers to a handler function,
* which then, based on conversion type part of the specifier, calls the
* appropriate function to handle the specific conversion.
* For specific functionality implemented, look at formatRe below, or look
* at the tests.
*/
goog.provide('goog.string.format');
goog.require('goog.string');
/**
* Performs sprintf-like conversion, ie. puts the values in a template.
* DO NOT use it instead of built-in conversions in simple cases such as
* 'Cost: %.2f' as it would introduce unneccessary latency oposed to
* 'Cost: ' + cost.toFixed(2).
* @param {string} formatString Template string containing % specifiers.
* @param {...string|number} var_args Values formatString is to be filled with.
* @return {string} Formatted string.
*/
goog.string.format = function(formatString, var_args) {
// Convert the arguments to an array (MDC recommended way).
var args = Array.prototype.slice.call(arguments);
// Try to get the template.
var template = args.shift();
if (typeof template == 'undefined') {
throw Error('[goog.string.format] Template required');
}
// This re is used for matching, it also defines what is supported.
var formatRe = /%([0\-\ \+]*)(\d+)?(\.(\d+))?([%sfdiu])/g;
/**
* Chooses which conversion function to call based on type conversion
* specifier.
* @param {string} match Contains the re matched string.
* @param {string} flags Formatting flags.
* @param {string} width Replacement string minimum width.
* @param {string} dotp Matched precision including a dot.
* @param {string} precision Specifies floating point precision.
* @param {string} type Type conversion specifier.
* @param {string} offset Matching location in the original string.
* @param {string} wholeString Has the actualString being searched.
* @return {string} Formatted parameter.
*/
function replacerDemuxer(match,
flags,
width,
dotp,
precision,
type,
offset,
wholeString) {
// The % is too simple and doesn't take an argument.
if (type == '%') {
return '%';
}
// Try to get the actual value from parent function.
var value = args.shift();
// If we didn't get any arguments, fail.
if (typeof value == 'undefined') {
throw Error('[goog.string.format] Not enough arguments');
}
// Patch the value argument to the beginning of our type specific call.
arguments[0] = value;
return goog.string.format.demuxes_[type].apply(null, arguments);
}
return template.replace(formatRe, replacerDemuxer);
};
/**
* Contains various conversion functions (to be filled in later on).
* @type {Object}
* @private
*/
goog.string.format.demuxes_ = {};
/**
* Processes %s conversion specifier.
* @param {string} value Contains the formatRe matched string.
* @param {string} flags Formatting flags.
* @param {string} width Replacement string minimum width.
* @param {string} dotp Matched precision including a dot.
* @param {string} precision Specifies floating point precision.
* @param {string} type Type conversion specifier.
* @param {string} offset Matching location in the original string.
* @param {string} wholeString Has the actualString being searched.
* @return {string} Replacement string.
*/
goog.string.format.demuxes_['s'] = function(value,
flags,
width,
dotp,
precision,
type,
offset,
wholeString) {
var replacement = value;
// If no padding is necessary we're done.
// The check for '' is necessary because Firefox incorrectly provides the
// empty string instead of undefined for non-participating capture groups,
// and isNaN('') == false.
if (isNaN(width) || width == '' || replacement.length >= width) {
return replacement;
}
// Otherwise we should find out where to put spaces.
if (flags.indexOf('-', 0) > -1) {
replacement =
replacement + goog.string.repeat(' ', width - replacement.length);
} else {
replacement =
goog.string.repeat(' ', width - replacement.length) + replacement;
}
return replacement;
};
/**
* Processes %f conversion specifier.
* @param {number} value Contains the formatRe matched string.
* @param {string} flags Formatting flags.
* @param {string} width Replacement string minimum width.
* @param {string} dotp Matched precision including a dot.
* @param {string} precision Specifies floating point precision.
* @param {string} type Type conversion specifier.
* @param {string} offset Matching location in the original string.
* @param {string} wholeString Has the actualString being searched.
* @return {string} Replacement string.
*/
goog.string.format.demuxes_['f'] = function(value,
flags,
width,
dotp,
precision,
type,
offset,
wholeString) {
var replacement = value.toString();
// The check for '' is necessary because Firefox incorrectly provides the
// empty string instead of undefined for non-participating capture groups,
// and isNaN('') == false.
if (!(isNaN(precision) || precision == '')) {
replacement = value.toFixed(precision);
}
// Generates sign string that will be attached to the replacement.
var sign;
if (value < 0) {
sign = '-';
} else if (flags.indexOf('+') >= 0) {
sign = '+';
} else if (flags.indexOf(' ') >= 0) {
sign = ' ';
} else {
sign = '';
}
if (value >= 0) {
replacement = sign + replacement;
}
// If no padding is neccessary we're done.
if (isNaN(width) || replacement.length >= width) {
return replacement;
}
// We need a clean signless replacement to start with
replacement = isNaN(precision) ?
Math.abs(value).toString() :
Math.abs(value).toFixed(precision);
var padCount = width - replacement.length - sign.length;
// Find out which side to pad, and if it's left side, then which character to
// pad, and set the sign on the left and padding in the middle.
if (flags.indexOf('-', 0) >= 0) {
replacement = sign + replacement + goog.string.repeat(' ', padCount);
} else {
// Decides which character to pad.
var paddingChar = (flags.indexOf('0', 0) >= 0) ? '0' : ' ';
replacement =
sign + goog.string.repeat(paddingChar, padCount) + replacement;
}
return replacement;
};
/**
* Processes %d conversion specifier.
* @param {string} value Contains the formatRe matched string.
* @param {string} flags Formatting flags.
* @param {string} width Replacement string minimum width.
* @param {string} dotp Matched precision including a dot.
* @param {string} precision Specifies floating point precision.
* @param {string} type Type conversion specifier.
* @param {string} offset Matching location in the original string.
* @param {string} wholeString Has the actualString being searched.
* @return {string} Replacement string.
*/
goog.string.format.demuxes_['d'] = function(value,
flags,
width,
dotp,
precision,
type,
offset,
wholeString) {
return goog.string.format.demuxes_['f'](
parseInt(value, 10) /* value */,
flags, width, dotp, 0 /* precision */,
type, offset, wholeString);
};
// These are additional aliases, for integer conversion.
goog.string.format.demuxes_['i'] = goog.string.format.demuxes_['d'];
goog.string.format.demuxes_['u'] = goog.string.format.demuxes_['d'];

View File

@@ -0,0 +1,38 @@
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Defines an interface for serializing objects into strings.
*/
goog.provide('goog.string.Stringifier');
/**
* An interface for serializing objects into strings.
* @interface
*/
goog.string.Stringifier = function() {};
/**
* Serializes an object or a value to a string.
* Agnostic to the particular format of object and string.
*
* @param {*} object The object to stringify.
* @return {string} A string representation of the input.
*/
goog.string.Stringifier.prototype.stringify;