1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
|
// 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 Utilities for string manipulation.
* @author arv@google.com (Erik Arvidsson)
*/
/**
* Namespace for string utilities
*/
goog.provide('goog.string');
goog.provide('goog.string.Unicode');
/**
* @define {boolean} Enables HTML escaping of lowercase letter "e" which helps
* with detection of double-escaping as this letter is frequently used.
*/
goog.define('goog.string.DETECT_DOUBLE_ESCAPING', false);
/**
* @define {boolean} Whether to force non-dom html unescaping.
*/
goog.define('goog.string.FORCE_NON_DOM_HTML_UNESCAPING', false);
/**
* Common Unicode string characters.
* @enum {string}
*/
goog.string.Unicode = {
NBSP: '\xa0'
};
/**
* Fast prefix-checker.
* @param {string} str The string to check.
* @param {string} prefix A string to look for at the start of {@code str}.
* @return {boolean} True if {@code str} begins with {@code prefix}.
*/
goog.string.startsWith = function(str, prefix) {
return str.lastIndexOf(prefix, 0) == 0;
};
/**
* Fast suffix-checker.
* @param {string} str The string to check.
* @param {string} suffix A string to look for at the end of {@code str}.
* @return {boolean} True if {@code str} ends with {@code suffix}.
*/
goog.string.endsWith = function(str, suffix) {
var l = str.length - suffix.length;
return l >= 0 && str.indexOf(suffix, l) == l;
};
/**
* Case-insensitive prefix-checker.
* @param {string} str The string to check.
* @param {string} prefix A string to look for at the end of {@code str}.
* @return {boolean} True if {@code str} begins with {@code prefix} (ignoring
* case).
*/
goog.string.caseInsensitiveStartsWith = function(str, prefix) {
return goog.string.caseInsensitiveCompare(
prefix, str.substr(0, prefix.length)) == 0;
};
/**
* Case-insensitive suffix-checker.
* @param {string} str The string to check.
* @param {string} suffix A string to look for at the end of {@code str}.
* @return {boolean} True if {@code str} ends with {@code suffix} (ignoring
* case).
*/
goog.string.caseInsensitiveEndsWith = function(str, suffix) {
return (
goog.string.caseInsensitiveCompare(
suffix, str.substr(str.length - suffix.length, suffix.length)) == 0);
};
/**
* Case-insensitive equality checker.
* @param {string} str1 First string to check.
* @param {string} str2 Second string to check.
* @return {boolean} True if {@code str1} and {@code str2} are the same string,
* ignoring case.
*/
goog.string.caseInsensitiveEquals = function(str1, str2) {
return str1.toLowerCase() == str2.toLowerCase();
};
/**
* Does simple python-style string substitution.
* subs("foo%s hot%s", "bar", "dog") becomes "foobar hotdog".
* @param {string} str The string containing the pattern.
* @param {...*} var_args The items to substitute into the pattern.
* @return {string} A copy of {@code str} in which each occurrence of
* {@code %s} has been replaced an argument from {@code var_args}.
*/
goog.string.subs = function(str, var_args) {
var splitParts = str.split('%s');
var returnString = '';
var subsArguments = Array.prototype.slice.call(arguments, 1);
while (subsArguments.length &&
// Replace up to the last split part. We are inserting in the
// positions between split parts.
splitParts.length > 1) {
returnString += splitParts.shift() + subsArguments.shift();
}
return returnString + splitParts.join('%s'); // Join unused '%s'
};
/**
* Converts multiple whitespace chars (spaces, non-breaking-spaces, new lines
* and tabs) to a single space, and strips leading and trailing whitespace.
* @param {string} str Input string.
* @return {string} A copy of {@code str} with collapsed whitespace.
*/
goog.string.collapseWhitespace = function(str) {
// Since IE doesn't include non-breaking-space (0xa0) in their \s character
// class (as required by section 7.2 of the ECMAScript spec), we explicitly
// include it in the regexp to enforce consistent cross-browser behavior.
return str.replace(/[\s\xa0]+/g, ' ').replace(/^\s+|\s+$/g, '');
};
/**
* Checks if a string is empty or contains only whitespaces.
* @param {string} str The string to check.
* @return {boolean} Whether {@code str} is empty or whitespace only.
*/
goog.string.isEmptyOrWhitespace = function(str) {
// testing length == 0 first is actually slower in all browsers (about the
// same in Opera).
// Since IE doesn't include non-breaking-space (0xa0) in their \s character
// class (as required by section 7.2 of the ECMAScript spec), we explicitly
// include it in the regexp to enforce consistent cross-browser behavior.
return /^[\s\xa0]*$/.test(str);
};
/**
* Checks if a string is empty.
* @param {string} str The string to check.
* @return {boolean} Whether {@code str} is empty.
*/
goog.string.isEmptyString = function(str) {
return str.length == 0;
};
/**
* Checks if a string is empty or contains only whitespaces.
*
* @param {string} str The string to check.
* @return {boolean} Whether {@code str} is empty or whitespace only.
* @deprecated Use goog.string.isEmptyOrWhitespace instead.
*/
goog.string.isEmpty = goog.string.isEmptyOrWhitespace;
/**
* Checks if a string is null, undefined, empty or contains only whitespaces.
* @param {*} str The string to check.
* @return {boolean} Whether {@code str} is null, undefined, empty, or
* whitespace only.
* @deprecated Use goog.string.isEmptyOrWhitespace(goog.string.makeSafe(str))
* instead.
*/
goog.string.isEmptyOrWhitespaceSafe = function(str) {
return goog.string.isEmptyOrWhitespace(goog.string.makeSafe(str));
};
/**
* Checks if a string is null, undefined, empty or contains only whitespaces.
*
* @param {*} str The string to check.
* @return {boolean} Whether {@code str} is null, undefined, empty, or
* whitespace only.
* @deprecated Use goog.string.isEmptyOrWhitespace instead.
*/
goog.string.isEmptySafe = goog.string.isEmptyOrWhitespaceSafe;
/**
* Checks if a string is all breaking whitespace.
* @param {string} str The string to check.
* @return {boolean} Whether the string is all breaking whitespace.
*/
goog.string.isBreakingWhitespace = function(str) {
return !/[^\t\n\r ]/.test(str);
};
/**
* Checks if a string contains all letters.
* @param {string} str string to check.
* @return {boolean} True if {@code str} consists entirely of letters.
*/
goog.string.isAlpha = function(str) {
return !/[^a-zA-Z]/.test(str);
};
/**
* Checks if a string contains only numbers.
* @param {*} str string to check. If not a string, it will be
* casted to one.
* @return {boolean} True if {@code str} is numeric.
*/
goog.string.isNumeric = function(str) {
return !/[^0-9]/.test(str);
};
/**
* Checks if a string contains only numbers or letters.
* @param {string} str string to check.
* @return {boolean} True if {@code str} is alphanumeric.
*/
goog.string.isAlphaNumeric = function(str) {
return !/[^a-zA-Z0-9]/.test(str);
};
/**
* Checks if a character is a space character.
* @param {string} ch Character to check.
* @return {boolean} True if {@code ch} is a space.
*/
goog.string.isSpace = function(ch) {
return ch == ' ';
};
/**
* Checks if a character is a valid unicode character.
* @param {string} ch Character to check.
* @return {boolean} True if {@code ch} is a valid unicode character.
*/
goog.string.isUnicodeChar = function(ch) {
return ch.length == 1 && ch >= ' ' && ch <= '~' ||
ch >= '\u0080' && ch <= '\uFFFD';
};
/**
* Takes a string and replaces newlines with a space. Multiple lines are
* replaced with a single space.
* @param {string} str The string from which to strip newlines.
* @return {string} A copy of {@code str} stripped of newlines.
*/
goog.string.stripNewlines = function(str) {
return str.replace(/(\r\n|\r|\n)+/g, ' ');
};
/**
* Replaces Windows and Mac new lines with unix style: \r or \r\n with \n.
* @param {string} str The string to in which to canonicalize newlines.
* @return {string} {@code str} A copy of {@code} with canonicalized newlines.
*/
goog.string.canonicalizeNewlines = function(str) {
return str.replace(/(\r\n|\r|\n)/g, '\n');
};
/**
* Normalizes whitespace in a string, replacing all whitespace chars with
* a space.
* @param {string} str The string in which to normalize whitespace.
* @return {string} A copy of {@code str} with all whitespace normalized.
*/
goog.string.normalizeWhitespace = function(str) {
return str.replace(/\xa0|\s/g, ' ');
};
/**
* Normalizes spaces in a string, replacing all consecutive spaces and tabs
* with a single space. Replaces non-breaking space with a space.
* @param {string} str The string in which to normalize spaces.
* @return {string} A copy of {@code str} with all consecutive spaces and tabs
* replaced with a single space.
*/
goog.string.normalizeSpaces = function(str) {
return str.replace(/\xa0|[ \t]+/g, ' ');
};
/**
* Removes the breaking spaces from the left and right of the string and
* collapses the sequences of breaking spaces in the middle into single spaces.
* The original and the result strings render the same way in HTML.
* @param {string} str A string in which to collapse spaces.
* @return {string} Copy of the string with normalized breaking spaces.
*/
goog.string.collapseBreakingSpaces = function(str) {
return str.replace(/[\t\r\n ]+/g, ' ')
.replace(/^[\t\r\n ]+|[\t\r\n ]+$/g, '');
};
/**
* Trims white spaces to the left and right of a string.
* @param {string} str The string to trim.
* @return {string} A trimmed copy of {@code str}.
*/
goog.string.trim =
(goog.TRUSTED_SITE && String.prototype.trim) ? function(str) {
return str.trim();
} : function(str) {
// Since IE doesn't include non-breaking-space (0xa0) in their \s
// character class (as required by section 7.2 of the ECMAScript spec),
// we explicitly include it in the regexp to enforce consistent
// cross-browser behavior.
// NOTE: We don't use String#replace because it might have side effects
// causing this function to not compile to 0 bytes.
return /^[\s\xa0]*([\s\S]*?)[\s\xa0]*$/.exec(str)[1];
};
/**
* Trims whitespaces at the left end of a string.
* @param {string} str The string to left trim.
* @return {string} A trimmed copy of {@code str}.
*/
goog.string.trimLeft = function(str) {
// Since IE doesn't include non-breaking-space (0xa0) in their \s character
// class (as required by section 7.2 of the ECMAScript spec), we explicitly
// include it in the regexp to enforce consistent cross-browser behavior.
return str.replace(/^[\s\xa0]+/, '');
};
/**
* Trims whitespaces at the right end of a string.
* @param {string} str The string to right trim.
* @return {string} A trimmed copy of {@code str}.
*/
goog.string.trimRight = function(str) {
// Since IE doesn't include non-breaking-space (0xa0) in their \s character
// class (as required by section 7.2 of the ECMAScript spec), we explicitly
// include it in the regexp to enforce consistent cross-browser behavior.
return str.replace(/[\s\xa0]+$/, '');
};
/**
* A string comparator that ignores case.
* -1 = str1 less than str2
* 0 = str1 equals str2
* 1 = str1 greater than str2
*
* @param {string} str1 The string to compare.
* @param {string} str2 The string to compare {@code str1} to.
* @return {number} The comparator result, as described above.
*/
goog.string.caseInsensitiveCompare = function(str1, str2) {
var test1 = String(str1).toLowerCase();
var test2 = String(str2).toLowerCase();
if (test1 < test2) {
return -1;
} else if (test1 == test2) {
return 0;
} else {
return 1;
}
};
/**
* Compares two strings interpreting their numeric substrings as numbers.
*
* @param {string} str1 First string.
* @param {string} str2 Second string.
* @param {!RegExp} tokenizerRegExp Splits a string into substrings of
* non-negative integers, non-numeric characters and optionally fractional
* numbers starting with a decimal point.
* @return {number} Negative if str1 < str2, 0 is str1 == str2, positive if
* str1 > str2.
* @private
*/
goog.string.numberAwareCompare_ = function(str1, str2, tokenizerRegExp) {
if (str1 == str2) {
return 0;
}
if (!str1) {
return -1;
}
if (!str2) {
return 1;
}
// Using match to split the entire string ahead of time turns out to be faster
// for most inputs than using RegExp.exec or iterating over each character.
var tokens1 = str1.toLowerCase().match(tokenizerRegExp);
var tokens2 = str2.toLowerCase().match(tokenizerRegExp);
var count = Math.min(tokens1.length, tokens2.length);
for (var i = 0; i < count; i++) {
var a = tokens1[i];
var b = tokens2[i];
// Compare pairs of tokens, returning if one token sorts before the other.
if (a != b) {
// Only if both tokens are integers is a special comparison required.
// Decimal numbers are sorted as strings (e.g., '.09' < '.1').
var num1 = parseInt(a, 10);
if (!isNaN(num1)) {
var num2 = parseInt(b, 10);
if (!isNaN(num2) && num1 - num2) {
return num1 - num2;
}
}
return a < b ? -1 : 1;
}
}
// If one string is a substring of the other, the shorter string sorts first.
if (tokens1.length != tokens2.length) {
return tokens1.length - tokens2.length;
}
// The two strings must be equivalent except for case (perfect equality is
// tested at the head of the function.) Revert to default ASCII string
// comparison to stabilize the sort.
return str1 < str2 ? -1 : 1;
};
/**
* String comparison function that handles non-negative integer numbers in a
* way humans might expect. Using this function, the string 'File 2.jpg' sorts
* before 'File 10.jpg', and 'Version 1.9' before 'Version 1.10'. The comparison
* is mostly case-insensitive, though strings that are identical except for case
* are sorted with the upper-case strings before lower-case.
*
* This comparison function is up to 50x slower than either the default or the
* case-insensitive compare. It should not be used in time-critical code, but
* should be fast enough to sort several hundred short strings (like filenames)
* with a reasonable delay.
*
* @param {string} str1 The string to compare in a numerically sensitive way.
* @param {string} str2 The string to compare {@code str1} to.
* @return {number} less than 0 if str1 < str2, 0 if str1 == str2, greater than
* 0 if str1 > str2.
*/
goog.string.intAwareCompare = function(str1, str2) {
return goog.string.numberAwareCompare_(str1, str2, /\d+|\D+/g);
};
/**
* String comparison function that handles non-negative integer and fractional
* numbers in a way humans might expect. Using this function, the string
* 'File 2.jpg' sorts before 'File 10.jpg', and '3.14' before '3.2'. Equivalent
* to {@link goog.string.intAwareCompare} apart from the way how it interprets
* dots.
*
* @param {string} str1 The string to compare in a numerically sensitive way.
* @param {string} str2 The string to compare {@code str1} to.
* @return {number} less than 0 if str1 < str2, 0 if str1 == str2, greater than
* 0 if str1 > str2.
*/
goog.string.floatAwareCompare = function(str1, str2) {
return goog.string.numberAwareCompare_(str1, str2, /\d+|\.\d+|\D+/g);
};
/**
* Alias for {@link goog.string.floatAwareCompare}.
*
* @param {string} str1
* @param {string} str2
* @return {number}
*/
goog.string.numerateCompare = goog.string.floatAwareCompare;
/**
* URL-encodes a string
* @param {*} str The string to url-encode.
* @return {string} An encoded copy of {@code str} that is safe for urls.
* Note that '#', ':', and other characters used to delimit portions
* of URLs *will* be encoded.
*/
goog.string.urlEncode = function(str) {
return encodeURIComponent(String(str));
};
/**
* URL-decodes the string. We need to specially handle '+'s because
* the javascript library doesn't convert them to spaces.
* @param {string} str The string to url decode.
* @return {string} The decoded {@code str}.
*/
goog.string.urlDecode = function(str) {
return decodeURIComponent(str.replace(/\+/g, ' '));
};
/**
* Converts \n to <br>s or <br />s.
* @param {string} str The string in which to convert newlines.
* @param {boolean=} opt_xml Whether to use XML compatible tags.
* @return {string} A copy of {@code str} with converted newlines.
*/
goog.string.newLineToBr = function(str, opt_xml) {
return str.replace(/(\r\n|\r|\n)/g, opt_xml ? '<br />' : '<br>');
};
/**
* Escapes double quote '"' and single quote '\'' characters in addition to
* '&', '<', and '>' so that a string can be included in an HTML tag attribute
* value within double or single quotes.
*
* It should be noted that > doesn't need to be escaped for the HTML or XML to
* be valid, but it has been decided to escape it for consistency with other
* implementations.
*
* With goog.string.DETECT_DOUBLE_ESCAPING, this function escapes also the
* lowercase letter "e".
*
* NOTE(user):
* HtmlEscape is often called during the generation of large blocks of HTML.
* Using statics for the regular expressions and strings is an optimization
* that can more than half the amount of time IE spends in this function for
* large apps, since strings and regexes both contribute to GC allocations.
*
* Testing for the presence of a character before escaping increases the number
* of function calls, but actually provides a speed increase for the average
* case -- since the average case often doesn't require the escaping of all 4
* characters and indexOf() is much cheaper than replace().
* The worst case does suffer slightly from the additional calls, therefore the
* opt_isLikelyToContainHtmlChars option has been included for situations
* where all 4 HTML entities are very likely to be present and need escaping.
*
* Some benchmarks (times tended to fluctuate +-0.05ms):
* FireFox IE6
* (no chars / average (mix of cases) / all 4 chars)
* no checks 0.13 / 0.22 / 0.22 0.23 / 0.53 / 0.80
* indexOf 0.08 / 0.17 / 0.26 0.22 / 0.54 / 0.84
* indexOf + re test 0.07 / 0.17 / 0.28 0.19 / 0.50 / 0.85
*
* An additional advantage of checking if replace actually needs to be called
* is a reduction in the number of object allocations, so as the size of the
* application grows the difference between the various methods would increase.
*
* @param {string} str string to be escaped.
* @param {boolean=} opt_isLikelyToContainHtmlChars Don't perform a check to see
* if the character needs replacing - use this option if you expect each of
* the characters to appear often. Leave false if you expect few html
* characters to occur in your strings, such as if you are escaping HTML.
* @return {string} An escaped copy of {@code str}.
*/
goog.string.htmlEscape = function(str, opt_isLikelyToContainHtmlChars) {
if (opt_isLikelyToContainHtmlChars) {
str = str.replace(goog.string.AMP_RE_, '&')
.replace(goog.string.LT_RE_, '<')
.replace(goog.string.GT_RE_, '>')
.replace(goog.string.QUOT_RE_, '"')
.replace(goog.string.SINGLE_QUOTE_RE_, ''')
.replace(goog.string.NULL_RE_, '�');
if (goog.string.DETECT_DOUBLE_ESCAPING) {
str = str.replace(goog.string.E_RE_, 'e');
}
return str;
} else {
// quick test helps in the case when there are no chars to replace, in
// worst case this makes barely a difference to the time taken
if (!goog.string.ALL_RE_.test(str)) return str;
// str.indexOf is faster than regex.test in this case
if (str.indexOf('&') != -1) {
str = str.replace(goog.string.AMP_RE_, '&');
}
if (str.indexOf('<') != -1) {
str = str.replace(goog.string.LT_RE_, '<');
}
if (str.indexOf('>') != -1) {
str = str.replace(goog.string.GT_RE_, '>');
}
if (str.indexOf('"') != -1) {
str = str.replace(goog.string.QUOT_RE_, '"');
}
if (str.indexOf('\'') != -1) {
str = str.replace(goog.string.SINGLE_QUOTE_RE_, ''');
}
if (str.indexOf('\x00') != -1) {
str = str.replace(goog.string.NULL_RE_, '�');
}
if (goog.string.DETECT_DOUBLE_ESCAPING && str.indexOf('e') != -1) {
str = str.replace(goog.string.E_RE_, 'e');
}
return str;
}
};
/**
* Regular expression that matches an ampersand, for use in escaping.
* @const {!RegExp}
* @private
*/
goog.string.AMP_RE_ = /&/g;
/**
* Regular expression that matches a less than sign, for use in escaping.
* @const {!RegExp}
* @private
*/
goog.string.LT_RE_ = /</g;
/**
* Regular expression that matches a greater than sign, for use in escaping.
* @const {!RegExp}
* @private
*/
goog.string.GT_RE_ = />/g;
/**
* Regular expression that matches a double quote, for use in escaping.
* @const {!RegExp}
* @private
*/
goog.string.QUOT_RE_ = /"/g;
/**
* Regular expression that matches a single quote, for use in escaping.
* @const {!RegExp}
* @private
*/
goog.string.SINGLE_QUOTE_RE_ = /'/g;
/**
* Regular expression that matches null character, for use in escaping.
* @const {!RegExp}
* @private
*/
goog.string.NULL_RE_ = /\x00/g;
/**
* Regular expression that matches a lowercase letter "e", for use in escaping.
* @const {!RegExp}
* @private
*/
goog.string.E_RE_ = /e/g;
/**
* Regular expression that matches any character that needs to be escaped.
* @const {!RegExp}
* @private
*/
goog.string.ALL_RE_ =
(goog.string.DETECT_DOUBLE_ESCAPING ? /[\x00&<>"'e]/ : /[\x00&<>"']/);
/**
* Unescapes an HTML string.
*
* @param {string} str The string to unescape.
* @return {string} An unescaped copy of {@code str}.
*/
goog.string.unescapeEntities = function(str) {
if (goog.string.contains(str, '&')) {
// We are careful not to use a DOM if we do not have one or we explicitly
// requested non-DOM html unescaping.
if (!goog.string.FORCE_NON_DOM_HTML_UNESCAPING &&
'document' in goog.global) {
return goog.string.unescapeEntitiesUsingDom_(str);
} else {
// Fall back on pure XML entities
return goog.string.unescapePureXmlEntities_(str);
}
}
return str;
};
/**
* Unescapes a HTML string using the provided document.
*
* @param {string} str The string to unescape.
* @param {!Document} document A document to use in escaping the string.
* @return {string} An unescaped copy of {@code str}.
*/
goog.string.unescapeEntitiesWithDocument = function(str, document) {
if (goog.string.contains(str, '&')) {
return goog.string.unescapeEntitiesUsingDom_(str, document);
}
return str;
};
/**
* Unescapes an HTML string using a DOM to resolve non-XML, non-numeric
* entities. This function is XSS-safe and whitespace-preserving.
* @private
* @param {string} str The string to unescape.
* @param {Document=} opt_document An optional document to use for creating
* elements. If this is not specified then the default window.document
* will be used.
* @return {string} The unescaped {@code str} string.
*/
goog.string.unescapeEntitiesUsingDom_ = function(str, opt_document) {
/** @type {!Object<string, string>} */
var seen = {'&': '&', '<': '<', '>': '>', '"': '"'};
var div;
if (opt_document) {
div = opt_document.createElement('div');
} else {
div = goog.global.document.createElement('div');
}
// Match as many valid entity characters as possible. If the actual entity
// happens to be shorter, it will still work as innerHTML will return the
// trailing characters unchanged. Since the entity characters do not include
// open angle bracket, there is no chance of XSS from the innerHTML use.
// Since no whitespace is passed to innerHTML, whitespace is preserved.
return str.replace(goog.string.HTML_ENTITY_PATTERN_, function(s, entity) {
// Check for cached entity.
var value = seen[s];
if (value) {
return value;
}
// Check for numeric entity.
if (entity.charAt(0) == '#') {
// Prefix with 0 so that hex entities (e.g. ) parse as hex numbers.
var n = Number('0' + entity.substr(1));
if (!isNaN(n)) {
value = String.fromCharCode(n);
}
}
// Fall back to innerHTML otherwise.
if (!value) {
// Append a non-entity character to avoid a bug in Webkit that parses
// an invalid entity at the end of innerHTML text as the empty string.
div.innerHTML = s + ' ';
// Then remove the trailing character from the result.
value = div.firstChild.nodeValue.slice(0, -1);
}
// Cache and return.
return seen[s] = value;
});
};
/**
* Unescapes XML entities.
* @private
* @param {string} str The string to unescape.
* @return {string} An unescaped copy of {@code str}.
*/
goog.string.unescapePureXmlEntities_ = function(str) {
return str.replace(/&([^;]+);/g, function(s, entity) {
switch (entity) {
case 'amp':
return '&';
case 'lt':
return '<';
case 'gt':
return '>';
case 'quot':
return '"';
default:
if (entity.charAt(0) == '#') {
// Prefix with 0 so that hex entities (e.g. ) parse as hex.
var n = Number('0' + entity.substr(1));
if (!isNaN(n)) {
return String.fromCharCode(n);
}
}
// For invalid entities we just return the entity
return s;
}
});
};
/**
* Regular expression that matches an HTML entity.
* See also HTML5: Tokenization / Tokenizing character references.
* @private
* @type {!RegExp}
*/
goog.string.HTML_ENTITY_PATTERN_ = /&([^;\s<&]+);?/g;
/**
* Do escaping of whitespace to preserve spatial formatting. We use character
* entity #160 to make it safer for xml.
* @param {string} str The string in which to escape whitespace.
* @param {boolean=} opt_xml Whether to use XML compatible tags.
* @return {string} An escaped copy of {@code str}.
*/
goog.string.whitespaceEscape = function(str, opt_xml) {
// This doesn't use goog.string.preserveSpaces for backwards compatibility.
return goog.string.newLineToBr(str.replace(/ /g, '  '), opt_xml);
};
/**
* Preserve spaces that would be otherwise collapsed in HTML by replacing them
* with non-breaking space Unicode characters.
* @param {string} str The string in which to preserve whitespace.
* @return {string} A copy of {@code str} with preserved whitespace.
*/
goog.string.preserveSpaces = function(str) {
return str.replace(/(^|[\n ]) /g, '$1' + goog.string.Unicode.NBSP);
};
/**
* Strip quote characters around a string. The second argument is a string of
* characters to treat as quotes. This can be a single character or a string of
* multiple character and in that case each of those are treated as possible
* quote characters. For example:
*
* <pre>
* goog.string.stripQuotes('"abc"', '"`') --> 'abc'
* goog.string.stripQuotes('`abc`', '"`') --> 'abc'
* </pre>
*
* @param {string} str The string to strip.
* @param {string} quoteChars The quote characters to strip.
* @return {string} A copy of {@code str} without the quotes.
*/
goog.string.stripQuotes = function(str, quoteChars) {
var length = quoteChars.length;
for (var i = 0; i < length; i++) {
var quoteChar = length == 1 ? quoteChars : quoteChars.charAt(i);
if (str.charAt(0) == quoteChar && str.charAt(str.length - 1) == quoteChar) {
return str.substring(1, str.length - 1);
}
}
return str;
};
/**
* Truncates a string to a certain length and adds '...' if necessary. The
* length also accounts for the ellipsis, so a maximum length of 10 and a string
* 'Hello World!' produces 'Hello W...'.
* @param {string} str The string to truncate.
* @param {number} chars Max number of characters.
* @param {boolean=} opt_protectEscapedCharacters Whether to protect escaped
* characters from being cut off in the middle.
* @return {string} The truncated {@code str} string.
*/
goog.string.truncate = function(str, chars, opt_protectEscapedCharacters) {
if (opt_protectEscapedCharacters) {
str = goog.string.unescapeEntities(str);
}
if (str.length > chars) {
str = str.substring(0, chars - 3) + '...';
}
if (opt_protectEscapedCharacters) {
str = goog.string.htmlEscape(str);
}
return str;
};
/**
* Truncate a string in the middle, adding "..." if necessary,
* and favoring the beginning of the string.
* @param {string} str The string to truncate the middle of.
* @param {number} chars Max number of characters.
* @param {boolean=} opt_protectEscapedCharacters Whether to protect escaped
* characters from being cutoff in the middle.
* @param {number=} opt_trailingChars Optional number of trailing characters to
* leave at the end of the string, instead of truncating as close to the
* middle as possible.
* @return {string} A truncated copy of {@code str}.
*/
goog.string.truncateMiddle = function(
str, chars, opt_protectEscapedCharacters, opt_trailingChars) {
if (opt_protectEscapedCharacters) {
str = goog.string.unescapeEntities(str);
}
if (opt_trailingChars && str.length > chars) {
if (opt_trailingChars > chars) {
opt_trailingChars = chars;
}
var endPoint = str.length - opt_trailingChars;
var startPoint = chars - opt_trailingChars;
str = str.substring(0, startPoint) + '...' + str.substring(endPoint);
} else if (str.length > chars) {
// Favor the beginning of the string:
var half = Math.floor(chars / 2);
var endPos = str.length - half;
half += chars % 2;
str = str.substring(0, half) + '...' + str.substring(endPos);
}
if (opt_protectEscapedCharacters) {
str = goog.string.htmlEscape(str);
}
return str;
};
/**
* Special chars that need to be escaped for goog.string.quote.
* @private {!Object<string, string>}
*/
goog.string.specialEscapeChars_ = {
'\0': '\\0',
'\b': '\\b',
'\f': '\\f',
'\n': '\\n',
'\r': '\\r',
'\t': '\\t',
'\x0B': '\\x0B', // '\v' is not supported in JScript
'"': '\\"',
'\\': '\\\\',
// To support the use case of embedding quoted strings inside of script
// tags, we have to make sure HTML comments and opening/closing script tags do
// not appear in the resulting string. The specific strings that must be
// escaped are documented at:
// http://www.w3.org/TR/html51/semantics.html#restrictions-for-contents-of-script-elements
'<': '\x3c'
};
/**
* Character mappings used internally for goog.string.escapeChar.
* @private {!Object<string, string>}
*/
goog.string.jsEscapeCache_ = {
'\'': '\\\''
};
/**
* Encloses a string in double quotes and escapes characters so that the
* string is a valid JS string. The resulting string is safe to embed in
* `<script>` tags as "<" is escaped.
* @param {string} s The string to quote.
* @return {string} A copy of {@code s} surrounded by double quotes.
*/
goog.string.quote = function(s) {
s = String(s);
var sb = ['"'];
for (var i = 0; i < s.length; i++) {
var ch = s.charAt(i);
var cc = ch.charCodeAt(0);
sb[i + 1] = goog.string.specialEscapeChars_[ch] ||
((cc > 31 && cc < 127) ? ch : goog.string.escapeChar(ch));
}
sb.push('"');
return sb.join('');
};
/**
* Takes a string and returns the escaped string for that input string.
* @param {string} str The string to escape.
* @return {string} An escaped string representing {@code str}.
*/
goog.string.escapeString = function(str) {
var sb = [];
for (var i = 0; i < str.length; i++) {
sb[i] = goog.string.escapeChar(str.charAt(i));
}
return sb.join('');
};
/**
* Takes a character and returns the escaped string for that character. For
* example escapeChar(String.fromCharCode(15)) -> "\\x0E".
* @param {string} c The character to escape.
* @return {string} An escaped string representing {@code c}.
*/
goog.string.escapeChar = function(c) {
if (c in goog.string.jsEscapeCache_) {
return goog.string.jsEscapeCache_[c];
}
if (c in goog.string.specialEscapeChars_) {
return goog.string.jsEscapeCache_[c] = goog.string.specialEscapeChars_[c];
}
var rv = c;
var cc = c.charCodeAt(0);
if (cc > 31 && cc < 127) {
rv = c;
} else {
// tab is 9 but handled above
if (cc < 256) {
rv = '\\x';
if (cc < 16 || cc > 256) {
rv += '0';
}
} else {
rv = '\\u';
if (cc < 4096) { // \u1000
rv += '0';
}
}
rv += cc.toString(16).toUpperCase();
}
return goog.string.jsEscapeCache_[c] = rv;
};
/**
* Determines whether a string contains a substring.
* @param {string} str The string to search.
* @param {string} subString The substring to search for.
* @return {boolean} Whether {@code str} contains {@code subString}.
*/
goog.string.contains = function(str, subString) {
return str.indexOf(subString) != -1;
};
/**
* Determines whether a string contains a substring, ignoring case.
* @param {string} str The string to search.
* @param {string} subString The substring to search for.
* @return {boolean} Whether {@code str} contains {@code subString}.
*/
goog.string.caseInsensitiveContains = function(str, subString) {
return goog.string.contains(str.toLowerCase(), subString.toLowerCase());
};
/**
* Returns the non-overlapping occurrences of ss in s.
* If either s or ss evalutes to false, then returns zero.
* @param {string} s The string to look in.
* @param {string} ss The string to look for.
* @return {number} Number of occurrences of ss in s.
*/
goog.string.countOf = function(s, ss) {
return s && ss ? s.split(ss).length - 1 : 0;
};
/**
* Removes a substring of a specified length at a specific
* index in a string.
* @param {string} s The base string from which to remove.
* @param {number} index The index at which to remove the substring.
* @param {number} stringLength The length of the substring to remove.
* @return {string} A copy of {@code s} with the substring removed or the full
* string if nothing is removed or the input is invalid.
*/
goog.string.removeAt = function(s, index, stringLength) {
var resultStr = s;
// If the index is greater or equal to 0 then remove substring
if (index >= 0 && index < s.length && stringLength > 0) {
resultStr = s.substr(0, index) +
s.substr(index + stringLength, s.length - index - stringLength);
}
return resultStr;
};
/**
* Removes the first occurrence of a substring from a string.
* @param {string} str The base string from which to remove.
* @param {string} substr The string to remove.
* @return {string} A copy of {@code str} with {@code substr} removed or the
* full string if nothing is removed.
*/
goog.string.remove = function(str, substr) {
return str.replace(substr, '');
};
/**
* Removes all occurrences of a substring from a string.
* @param {string} s The base string from which to remove.
* @param {string} ss The string to remove.
* @return {string} A copy of {@code s} with {@code ss} removed or the full
* string if nothing is removed.
*/
goog.string.removeAll = function(s, ss) {
var re = new RegExp(goog.string.regExpEscape(ss), 'g');
return s.replace(re, '');
};
/**
* Replaces all occurrences of a substring of a string with a new substring.
* @param {string} s The base string from which to remove.
* @param {string} ss The string to replace.
* @param {string} replacement The replacement string.
* @return {string} A copy of {@code s} with {@code ss} replaced by
* {@code replacement} or the original string if nothing is replaced.
*/
goog.string.replaceAll = function(s, ss, replacement) {
var re = new RegExp(goog.string.regExpEscape(ss), 'g');
return s.replace(re, replacement.replace(/\$/g, '$$$$'));
};
/**
* Escapes characters in the string that are not safe to use in a RegExp.
* @param {*} s The string to escape. If not a string, it will be casted
* to one.
* @return {string} A RegExp safe, escaped copy of {@code s}.
*/
goog.string.regExpEscape = function(s) {
return String(s)
.replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1')
.replace(/\x08/g, '\\x08');
};
/**
* Repeats a string n times.
* @param {string} string The string to repeat.
* @param {number} length The number of times to repeat.
* @return {string} A string containing {@code length} repetitions of
* {@code string}.
*/
goog.string.repeat = (String.prototype.repeat) ? function(string, length) {
// The native method is over 100 times faster than the alternative.
return string.repeat(length);
} : function(string, length) {
return new Array(length + 1).join(string);
};
/**
* Pads number to given length and optionally rounds it to a given precision.
* For example:
* <pre>padNumber(1.25, 2, 3) -> '01.250'
* padNumber(1.25, 2) -> '01.25'
* padNumber(1.25, 2, 1) -> '01.3'
* padNumber(1.25, 0) -> '1.25'</pre>
*
* @param {number} num The number to pad.
* @param {number} length The desired length.
* @param {number=} opt_precision The desired precision.
* @return {string} {@code num} as a string with the given options.
*/
goog.string.padNumber = function(num, length, opt_precision) {
var s = goog.isDef(opt_precision) ? num.toFixed(opt_precision) : String(num);
var index = s.indexOf('.');
if (index == -1) {
index = s.length;
}
return goog.string.repeat('0', Math.max(0, length - index)) + s;
};
/**
* Returns a string representation of the given object, with
* null and undefined being returned as the empty string.
*
* @param {*} obj The object to convert.
* @return {string} A string representation of the {@code obj}.
*/
goog.string.makeSafe = function(obj) {
return obj == null ? '' : String(obj);
};
/**
* Concatenates string expressions. This is useful
* since some browsers are very inefficient when it comes to using plus to
* concat strings. Be careful when using null and undefined here since
* these will not be included in the result. If you need to represent these
* be sure to cast the argument to a String first.
* For example:
* <pre>buildString('a', 'b', 'c', 'd') -> 'abcd'
* buildString(null, undefined) -> ''
* </pre>
* @param {...*} var_args A list of strings to concatenate. If not a string,
* it will be casted to one.
* @return {string} The concatenation of {@code var_args}.
*/
goog.string.buildString = function(var_args) {
return Array.prototype.join.call(arguments, '');
};
/**
* Returns a string with at least 64-bits of randomness.
*
* Doesn't trust Javascript's random function entirely. Uses a combination of
* random and current timestamp, and then encodes the string in base-36 to
* make it shorter.
*
* @return {string} A random string, e.g. sn1s7vb4gcic.
*/
goog.string.getRandomString = function() {
var x = 2147483648;
return Math.floor(Math.random() * x).toString(36) +
Math.abs(Math.floor(Math.random() * x) ^ goog.now()).toString(36);
};
/**
* Compares two version numbers.
*
* @param {string|number} version1 Version of first item.
* @param {string|number} version2 Version of second item.
*
* @return {number} 1 if {@code version1} is higher.
* 0 if arguments are equal.
* -1 if {@code version2} is higher.
*/
goog.string.compareVersions = function(version1, version2) {
var order = 0;
// Trim leading and trailing whitespace and split the versions into
// subversions.
var v1Subs = goog.string.trim(String(version1)).split('.');
var v2Subs = goog.string.trim(String(version2)).split('.');
var subCount = Math.max(v1Subs.length, v2Subs.length);
// Iterate over the subversions, as long as they appear to be equivalent.
for (var subIdx = 0; order == 0 && subIdx < subCount; subIdx++) {
var v1Sub = v1Subs[subIdx] || '';
var v2Sub = v2Subs[subIdx] || '';
do {
// Split the subversions into pairs of numbers and qualifiers (like 'b').
// Two different RegExp objects are use to make it clear the code
// is side-effect free
var v1Comp = /(\d*)(\D*)(.*)/.exec(v1Sub) || ['', '', '', ''];
var v2Comp = /(\d*)(\D*)(.*)/.exec(v2Sub) || ['', '', '', ''];
// Break if there are no more matches.
if (v1Comp[0].length == 0 && v2Comp[0].length == 0) {
break;
}
// Parse the numeric part of the subversion. A missing number is
// equivalent to 0.
var v1CompNum = v1Comp[1].length == 0 ? 0 : parseInt(v1Comp[1], 10);
var v2CompNum = v2Comp[1].length == 0 ? 0 : parseInt(v2Comp[1], 10);
// Compare the subversion components. The number has the highest
// precedence. Next, if the numbers are equal, a subversion without any
// qualifier is always higher than a subversion with any qualifier. Next,
// the qualifiers are compared as strings.
order = goog.string.compareElements_(v1CompNum, v2CompNum) ||
goog.string.compareElements_(
v1Comp[2].length == 0, v2Comp[2].length == 0) ||
goog.string.compareElements_(v1Comp[2], v2Comp[2]);
// Stop as soon as an inequality is discovered.
v1Sub = v1Comp[3];
v2Sub = v2Comp[3];
} while (order == 0);
}
return order;
};
/**
* Compares elements of a version number.
*
* @param {string|number|boolean} left An element from a version number.
* @param {string|number|boolean} right An element from a version number.
*
* @return {number} 1 if {@code left} is higher.
* 0 if arguments are equal.
* -1 if {@code right} is higher.
* @private
*/
goog.string.compareElements_ = function(left, right) {
if (left < right) {
return -1;
} else if (left > right) {
return 1;
}
return 0;
};
/**
* String hash function similar to java.lang.String.hashCode().
* The hash code for a string is computed as
* s[0] * 31 ^ (n - 1) + s[1] * 31 ^ (n - 2) + ... + s[n - 1],
* where s[i] is the ith character of the string and n is the length of
* the string. We mod the result to make it between 0 (inclusive) and 2^32
* (exclusive).
* @param {string} str A string.
* @return {number} Hash value for {@code str}, between 0 (inclusive) and 2^32
* (exclusive). The empty string returns 0.
*/
goog.string.hashCode = function(str) {
var result = 0;
for (var i = 0; i < str.length; ++i) {
// Normalize to 4 byte range, 0 ... 2^32.
result = (31 * result + str.charCodeAt(i)) >>> 0;
}
return result;
};
/**
* The most recent unique ID. |0 is equivalent to Math.floor in this case.
* @type {number}
* @private
*/
goog.string.uniqueStringCounter_ = Math.random() * 0x80000000 | 0;
/**
* Generates and returns a string which is unique in the current document.
* This is useful, for example, to create unique IDs for DOM elements.
* @return {string} A unique id.
*/
goog.string.createUniqueString = function() {
return 'goog_' + goog.string.uniqueStringCounter_++;
};
/**
* Converts the supplied string to a number, which may be Infinity or NaN.
* This function strips whitespace: (toNumber(' 123') === 123)
* This function accepts scientific notation: (toNumber('1e1') === 10)
*
* This is better than Javascript's built-in conversions because, sadly:
* (Number(' ') === 0) and (parseFloat('123a') === 123)
*
* @param {string} str The string to convert.
* @return {number} The number the supplied string represents, or NaN.
*/
goog.string.toNumber = function(str) {
var num = Number(str);
if (num == 0 && goog.string.isEmptyOrWhitespace(str)) {
return NaN;
}
return num;
};
/**
* Returns whether the given string is lower camel case (e.g. "isFooBar").
*
* Note that this assumes the string is entirely letters.
* @see http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms
*
* @param {string} str String to test.
* @return {boolean} Whether the string is lower camel case.
*/
goog.string.isLowerCamelCase = function(str) {
return /^[a-z]+([A-Z][a-z]*)*$/.test(str);
};
/**
* Returns whether the given string is upper camel case (e.g. "FooBarBaz").
*
* Note that this assumes the string is entirely letters.
* @see http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms
*
* @param {string} str String to test.
* @return {boolean} Whether the string is upper camel case.
*/
goog.string.isUpperCamelCase = function(str) {
return /^([A-Z][a-z]*)+$/.test(str);
};
/**
* Converts a string from selector-case to camelCase (e.g. from
* "multi-part-string" to "multiPartString"), useful for converting
* CSS selectors and HTML dataset keys to their equivalent JS properties.
* @param {string} str The string in selector-case form.
* @return {string} The string in camelCase form.
*/
goog.string.toCamelCase = function(str) {
return String(str).replace(
/\-([a-z])/g, function(all, match) { return match.toUpperCase(); });
};
/**
* Converts a string from camelCase to selector-case (e.g. from
* "multiPartString" to "multi-part-string"), useful for converting JS
* style and dataset properties to equivalent CSS selectors and HTML keys.
* @param {string} str The string in camelCase form.
* @return {string} The string in selector-case form.
*/
goog.string.toSelectorCase = function(str) {
return String(str).replace(/([A-Z])/g, '-$1').toLowerCase();
};
/**
* Converts a string into TitleCase. First character of the string is always
* capitalized in addition to the first letter of every subsequent word.
* Words are delimited by one or more whitespaces by default. Custom delimiters
* can optionally be specified to replace the default, which doesn't preserve
* whitespace delimiters and instead must be explicitly included if needed.
*
* Default delimiter => " ":
* goog.string.toTitleCase('oneTwoThree') => 'OneTwoThree'
* goog.string.toTitleCase('one two three') => 'One Two Three'
* goog.string.toTitleCase(' one two ') => ' One Two '
* goog.string.toTitleCase('one_two_three') => 'One_two_three'
* goog.string.toTitleCase('one-two-three') => 'One-two-three'
*
* Custom delimiter => "_-.":
* goog.string.toTitleCase('oneTwoThree', '_-.') => 'OneTwoThree'
* goog.string.toTitleCase('one two three', '_-.') => 'One two three'
* goog.string.toTitleCase(' one two ', '_-.') => ' one two '
* goog.string.toTitleCase('one_two_three', '_-.') => 'One_Two_Three'
* goog.string.toTitleCase('one-two-three', '_-.') => 'One-Two-Three'
* goog.string.toTitleCase('one...two...three', '_-.') => 'One...Two...Three'
* goog.string.toTitleCase('one. two. three', '_-.') => 'One. two. three'
* goog.string.toTitleCase('one-two.three', '_-.') => 'One-Two.Three'
*
* @param {string} str String value in camelCase form.
* @param {string=} opt_delimiters Custom delimiter character set used to
* distinguish words in the string value. Each character represents a
* single delimiter. When provided, default whitespace delimiter is
* overridden and must be explicitly included if needed.
* @return {string} String value in TitleCase form.
*/
goog.string.toTitleCase = function(str, opt_delimiters) {
var delimiters = goog.isString(opt_delimiters) ?
goog.string.regExpEscape(opt_delimiters) :
'\\s';
// For IE8, we need to prevent using an empty character set. Otherwise,
// incorrect matching will occur.
delimiters = delimiters ? '|[' + delimiters + ']+' : '';
var regexp = new RegExp('(^' + delimiters + ')([a-z])', 'g');
return str.replace(
regexp, function(all, p1, p2) { return p1 + p2.toUpperCase(); });
};
/**
* Capitalizes a string, i.e. converts the first letter to uppercase
* and all other letters to lowercase, e.g.:
*
* goog.string.capitalize('one') => 'One'
* goog.string.capitalize('ONE') => 'One'
* goog.string.capitalize('one two') => 'One two'
*
* Note that this function does not trim initial whitespace.
*
* @param {string} str String value to capitalize.
* @return {string} String value with first letter in uppercase.
*/
goog.string.capitalize = function(str) {
return String(str.charAt(0)).toUpperCase() +
String(str.substr(1)).toLowerCase();
};
/**
* Parse a string in decimal or hexidecimal ('0xFFFF') form.
*
* To parse a particular radix, please use parseInt(string, radix) directly. See
* https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt
*
* This is a wrapper for the built-in parseInt function that will only parse
* numbers as base 10 or base 16. Some JS implementations assume strings
* starting with "0" are intended to be octal. ES3 allowed but discouraged
* this behavior. ES5 forbids it. This function emulates the ES5 behavior.
*
* For more information, see Mozilla JS Reference: http://goo.gl/8RiFj
*
* @param {string|number|null|undefined} value The value to be parsed.
* @return {number} The number, parsed. If the string failed to parse, this
* will be NaN.
*/
goog.string.parseInt = function(value) {
// Force finite numbers to strings.
if (isFinite(value)) {
value = String(value);
}
if (goog.isString(value)) {
// If the string starts with '0x' or '-0x', parse as hex.
return /^\s*-?0x/i.test(value) ? parseInt(value, 16) : parseInt(value, 10);
}
return NaN;
};
/**
* Splits a string on a separator a limited number of times.
*
* This implementation is more similar to Python or Java, where the limit
* parameter specifies the maximum number of splits rather than truncating
* the number of results.
*
* See http://docs.python.org/2/library/stdtypes.html#str.split
* See JavaDoc: http://goo.gl/F2AsY
* See Mozilla reference: http://goo.gl/dZdZs
*
* @param {string} str String to split.
* @param {string} separator The separator.
* @param {number} limit The limit to the number of splits. The resulting array
* will have a maximum length of limit+1. Negative numbers are the same
* as zero.
* @return {!Array<string>} The string, split.
*/
goog.string.splitLimit = function(str, separator, limit) {
var parts = str.split(separator);
var returnVal = [];
// Only continue doing this while we haven't hit the limit and we have
// parts left.
while (limit > 0 && parts.length) {
returnVal.push(parts.shift());
limit--;
}
// If there are remaining parts, append them to the end.
if (parts.length) {
returnVal.push(parts.join(separator));
}
return returnVal;
};
/**
* Finds the characters to the right of the last instance of any separator
*
* This function is similar to goog.string.path.baseName, except it can take a
* list of characters to split the string on. It will return the rightmost
* grouping of characters to the right of any separator as a left-to-right
* oriented string.
*
* @see goog.string.path.baseName
* @param {string} str The string
* @param {string|!Array<string>} separators A list of separator characters
* @return {string} The last part of the string with respect to the separators
*/
goog.string.lastComponent = function(str, separators) {
if (!separators) {
return str;
} else if (typeof separators == 'string') {
separators = [separators];
}
var lastSeparatorIndex = -1;
for (var i = 0; i < separators.length; i++) {
if (separators[i] == '') {
continue;
}
var currentSeparatorIndex = str.lastIndexOf(separators[i]);
if (currentSeparatorIndex > lastSeparatorIndex) {
lastSeparatorIndex = currentSeparatorIndex;
}
}
if (lastSeparatorIndex == -1) {
return str;
}
return str.slice(lastSeparatorIndex + 1);
};
/**
* Computes the Levenshtein edit distance between two strings.
* @param {string} a
* @param {string} b
* @return {number} The edit distance between the two strings.
*/
goog.string.editDistance = function(a, b) {
var v0 = [];
var v1 = [];
if (a == b) {
return 0;
}
if (!a.length || !b.length) {
return Math.max(a.length, b.length);
}
for (var i = 0; i < b.length + 1; i++) {
v0[i] = i;
}
for (var i = 0; i < a.length; i++) {
v1[0] = i + 1;
for (var j = 0; j < b.length; j++) {
var cost = Number(a[i] != b[j]);
// Cost for the substring is the minimum of adding one character, removing
// one character, or a swap.
v1[j + 1] = Math.min(v1[j] + 1, v0[j + 1] + 1, v0[j] + cost);
}
for (var j = 0; j < v0.length; j++) {
v0[j] = v1[j];
}
}
return v1[b.length];
};
|