1// Copyright 2006 The Closure Library Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS-IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/**
16 * @fileoverview Utilities for string manipulation.
17 */
18
19
20/**
21 * Namespace for string utilities
22 */
23goog.provide('goog.string');
24
25
26/**
27 * Does simple python-style string substitution.
28 * subs("foo%s hot%s", "bar", "dog") becomes "foobar hotdog".
29 * @param {string} str The string containing the pattern.
30 * @param {...*} var_args The items to substitute into the pattern.
31 * @return {string} A copy of {@code str} in which each occurrence of
32 *     {@code %s} has been replaced an argument from {@code var_args}.
33 */
34goog.string.subs = function(str, var_args) {
35  var splitParts = str.split('%s');
36  var returnString = '';
37
38  var subsArguments = Array.prototype.slice.call(arguments, 1);
39  while (subsArguments.length &&
40         // Replace up to the last split part. We are inserting in the
41         // positions between split parts.
42         splitParts.length > 1) {
43    returnString += splitParts.shift() + subsArguments.shift();
44  }
45
46  return returnString + splitParts.join('%s'); // Join unused '%s'
47};
48