/files/phpjs/0.1/strings/substr.js

https://gitlab.com/Mirros/jsdelivr · JavaScript · 119 lines · 74 code · 3 blank · 42 comment · 31 complexity · 285bc4912caf830bffbfdd2415b34409 MD5 · raw file

  1. function substr (str, start, len) {
  2. // Returns part of a string
  3. //
  4. // version: 909.322
  5. // discuss at: http://phpjs.org/functions/substr
  6. // + original by: Martijn Wieringa
  7. // + bugfixed by: T.Wild
  8. // + tweaked by: Onno Marsman
  9. // + revised by: Theriault
  10. // + improved by: Brett Zamir (http://brett-zamir.me)
  11. // % note 1: Handles rare Unicode characters if 'unicode.semantics' ini (PHP6) is set to 'on'
  12. // * example 1: substr('abcdef', 0, -1);
  13. // * returns 1: 'abcde'
  14. // * example 2: substr(2, 0, -6);
  15. // * returns 2: false
  16. // * example 3: ini_set('unicode.semantics', 'on');
  17. // * example 3: substr('a\uD801\uDC00', 0, -1);
  18. // * returns 3: 'a'
  19. // * example 4: ini_set('unicode.semantics', 'on');
  20. // * example 4: substr('a\uD801\uDC00', 0, 2);
  21. // * returns 4: 'a\uD801\uDC00'
  22. // * example 5: ini_set('unicode.semantics', 'on');
  23. // * example 5: substr('a\uD801\uDC00', -1, 1);
  24. // * returns 5: '\uD801\uDC00'
  25. // * example 6: ini_set('unicode.semantics', 'on');
  26. // * example 6: substr('a\uD801\uDC00z\uD801\uDC00', -3, 2);
  27. // * returns 6: '\uD801\uDC00z'
  28. // * example 7: ini_set('unicode.semantics', 'on');
  29. // * example 7: substr('a\uD801\uDC00z\uD801\uDC00', -3, -1)
  30. // * returns 7: '\uD801\uDC00z'
  31. // Add: (?) Use unicode.runtime_encoding (e.g., with string wrapped in "binary" or "Binary" class) to
  32. // allow access of binary (see file_get_contents()) by: charCodeAt(x) & 0xFF (see https://developer.mozilla.org/En/Using_XMLHttpRequest ) or require conversion first?
  33. var i = 0,
  34. allBMP = true,
  35. es = 0,
  36. el = 0,
  37. se = 0,
  38. ret = '';
  39. str += '';
  40. var end = str.length;
  41. // BEGIN REDUNDANT
  42. this.php_js = this.php_js || {};
  43. this.php_js.ini = this.php_js.ini || {};
  44. // END REDUNDANT
  45. switch ((this.php_js.ini['unicode.semantics'] && this.php_js.ini['unicode.semantics'].local_value.toLowerCase())) {
  46. case 'on':
  47. // Full-blown Unicode including non-Basic-Multilingual-Plane characters
  48. // strlen()
  49. for (i = 0; i < str.length; i++) {
  50. if (/[\uD800-\uDBFF]/.test(str.charAt(i)) && /[\uDC00-\uDFFF]/.test(str.charAt(i + 1))) {
  51. allBMP = false;
  52. break;
  53. }
  54. }
  55. if (!allBMP) {
  56. if (start < 0) {
  57. for (i = end - 1, es = (start += end); i >= es; i--) {
  58. if (/[\uDC00-\uDFFF]/.test(str.charAt(i)) && /[\uD800-\uDBFF]/.test(str.charAt(i - 1))) {
  59. start--;
  60. es--;
  61. }
  62. }
  63. } else {
  64. var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
  65. while ((surrogatePairs.exec(str)) != null) {
  66. var li = surrogatePairs.lastIndex;
  67. if (li - 2 < start) {
  68. start++;
  69. } else {
  70. break;
  71. }
  72. }
  73. }
  74. if (start >= end || start < 0) {
  75. return false;
  76. }
  77. if (len < 0) {
  78. for (i = end - 1, el = (end += len); i >= el; i--) {
  79. if (/[\uDC00-\uDFFF]/.test(str.charAt(i)) && /[\uD800-\uDBFF]/.test(str.charAt(i - 1))) {
  80. end--;
  81. el--;
  82. }
  83. }
  84. if (start > end) {
  85. return false;
  86. }
  87. return str.slice(start, end);
  88. } else {
  89. se = start + len;
  90. for (i = start; i < se; i++) {
  91. ret += str.charAt(i);
  92. if (/[\uD800-\uDBFF]/.test(str.charAt(i)) && /[\uDC00-\uDFFF]/.test(str.charAt(i + 1))) {
  93. se++; // Go one further, since one of the "characters" is part of a surrogate pair
  94. }
  95. }
  96. return ret;
  97. }
  98. break;
  99. }
  100. // Fall-through
  101. case 'off':
  102. // assumes there are no non-BMP characters;
  103. // if there may be such characters, then it is best to turn it on (critical in true XHTML/XML)
  104. default:
  105. if (start < 0) {
  106. start += end;
  107. }
  108. end = typeof len === 'undefined' ? end : (len < 0 ? len + end : len + start);
  109. // PHP returns false if start does not fall within the string.
  110. // PHP returns false if the calculated end comes before the calculated start.
  111. // PHP returns an empty string if start and end are the same.
  112. // Otherwise, PHP returns the portion of the string from start to end.
  113. return start >= str.length || start < 0 || start > end ? !1 : str.slice(start, end);
  114. }
  115. return undefined; // Please Netbeans
  116. }