102 | | if (named) { |
103 | | return fmt.replace(/%\(\w+\)s/, function(match){return String(obj[match.slice(2,-2)])}); |
104 | | } else { |
105 | | return fmt.replace(/%s/, function(match){return String(obj.shift())}); |
106 | | } |
| 102 | var result = []; |
| 103 | var names = []; |
| 104 | var str = fmt; |
| 105 | if(named) { |
| 106 | str = fmt.replace(/%\(\w+\)s/, "%s"); |
| 107 | names = fmt.match(/%\(\w+\)s/g); |
| 108 | } |
| 109 | var parts = str.split("%s"); |
| 110 | if(named) { |
| 111 | for(int i = 0; i < parts.length - 1; ++i) { |
| 112 | result[result.length] = parts[i]; |
| 113 | result[result.length] = String(obj[names[i].slice(2,-2)]); |
| 114 | } |
| 115 | } else { |
| 116 | for(int i = 0; i < parts.length - 1; ++i) { |
| 117 | result[result.length] = parts[i]; |
| 118 | result[result.length] = String(obj[i]); |
| 119 | } |
| 120 | } |
| 121 | result[result.length] = parts[names.length]; |
| 122 | return result.join(""); |