Updated
This commit is contained in:
@@ -0,0 +1,233 @@
|
||||
// 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] : '';
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @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_ + '])$');
|
||||
|
||||
|
||||
/**
|
||||
* @type {string}
|
||||
* @const
|
||||
* @private
|
||||
*/
|
||||
goog.string.linkify.ACCEPTABLE_URL_CHARS_ =
|
||||
goog.string.linkify.ENDING_PUNCTUATION_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');
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
// 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;
|
||||
@@ -0,0 +1,146 @@
|
||||
// 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);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* 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;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* 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(arg, '/')) {
|
||||
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
@@ -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_;
|
||||
};
|
||||
@@ -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'];
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
// 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;
|
||||
Reference in New Issue
Block a user