PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/twig/node_modules/phpjs/functions/strings/addcslashes.js

https://gitlab.com/rickoverman/doo-cli
JavaScript | 158 lines | 136 code | 4 blank | 18 comment | 33 complexity | 917aec9efb233843c31895064c440fcc MD5 | raw file
  1. function addcslashes(str, charlist) {
  2. // discuss at: http://phpjs.org/functions/addcslashes/
  3. // original by: Brett Zamir (http://brett-zamir.me)
  4. // note: We show double backslashes in the return value example code below because a JavaScript string will not
  5. // note: render them as backslashes otherwise
  6. // example 1: addcslashes('foo[ ]', 'A..z'); // Escape all ASCII within capital A to lower z range, including square brackets
  7. // returns 1: "\\f\\o\\o\\[ \\]"
  8. // example 2: addcslashes("zoo['.']", 'z..A'); // Only escape z, period, and A here since not a lower-to-higher range
  9. // returns 2: "\\zoo['\\.']"
  10. // example 3: addcslashes("@a\u0000\u0010\u00A9", "\0..\37!@\177..\377"); // Escape as octals those specified and less than 32 (0x20) or greater than 126 (0x7E), but not otherwise
  11. // returns 3: '\\@a\\000\\020\\302\\251'
  12. // example 4: addcslashes("\u0020\u007E", "\40..\175"); // Those between 32 (0x20 or 040) and 126 (0x7E or 0176) decimal value will be backslashed if specified (not octalized)
  13. // returns 4: '\\ ~'
  14. // example 5: addcslashes("\r\u0007\n", '\0..\37'); // Recognize C escape sequences if specified
  15. // returns 5: "\\r\\a\\n"
  16. // example 6: addcslashes("\r\u0007\n", '\0'); // Do not recognize C escape sequences if not specified
  17. // returns 6: "\r\u0007\n"
  18. var target = '',
  19. chrs = [],
  20. i = 0,
  21. j = 0,
  22. c = '',
  23. next = '',
  24. rangeBegin = '',
  25. rangeEnd = '',
  26. chr = '',
  27. begin = 0,
  28. end = 0,
  29. octalLength = 0,
  30. postOctalPos = 0,
  31. cca = 0,
  32. escHexGrp = [],
  33. encoded = '',
  34. percentHex = /%([\dA-Fa-f]+)/g;
  35. var _pad = function(n, c) {
  36. if ((n = n + '')
  37. .length < c) {
  38. return new Array(++c - n.length)
  39. .join('0') + n;
  40. }
  41. return n;
  42. };
  43. for (i = 0; i < charlist.length; i++) {
  44. c = charlist.charAt(i);
  45. next = charlist.charAt(i + 1);
  46. if (c === '\\' && next && (/\d/)
  47. .test(next)) { // Octal
  48. rangeBegin = charlist.slice(i + 1)
  49. .match(/^\d+/)[0];
  50. octalLength = rangeBegin.length;
  51. postOctalPos = i + octalLength + 1;
  52. if (charlist.charAt(postOctalPos) + charlist.charAt(postOctalPos + 1) === '..') { // Octal begins range
  53. begin = rangeBegin.charCodeAt(0);
  54. if ((/\\\d/)
  55. .test(charlist.charAt(postOctalPos + 2) + charlist.charAt(postOctalPos + 3))) { // Range ends with octal
  56. rangeEnd = charlist.slice(postOctalPos + 3)
  57. .match(/^\d+/)[0];
  58. i += 1; // Skip range end backslash
  59. } else if (charlist.charAt(postOctalPos + 2)) { // Range ends with character
  60. rangeEnd = charlist.charAt(postOctalPos + 2);
  61. } else {
  62. throw 'Range with no end point';
  63. }
  64. end = rangeEnd.charCodeAt(0);
  65. if (end > begin) { // Treat as a range
  66. for (j = begin; j <= end; j++) {
  67. chrs.push(String.fromCharCode(j));
  68. }
  69. } else { // Supposed to treat period, begin and end as individual characters only, not a range
  70. chrs.push('.', rangeBegin, rangeEnd);
  71. }
  72. i += rangeEnd.length + 2; // Skip dots and range end (already skipped range end backslash if present)
  73. } else { // Octal is by itself
  74. chr = String.fromCharCode(parseInt(rangeBegin, 8));
  75. chrs.push(chr);
  76. }
  77. i += octalLength; // Skip range begin
  78. } else if (next + charlist.charAt(i + 2) === '..') { // Character begins range
  79. rangeBegin = c;
  80. begin = rangeBegin.charCodeAt(0);
  81. if ((/\\\d/)
  82. .test(charlist.charAt(i + 3) + charlist.charAt(i + 4))) { // Range ends with octal
  83. rangeEnd = charlist.slice(i + 4)
  84. .match(/^\d+/)[0];
  85. i += 1; // Skip range end backslash
  86. } else if (charlist.charAt(i + 3)) { // Range ends with character
  87. rangeEnd = charlist.charAt(i + 3);
  88. } else {
  89. throw 'Range with no end point';
  90. }
  91. end = rangeEnd.charCodeAt(0);
  92. if (end > begin) { // Treat as a range
  93. for (j = begin; j <= end; j++) {
  94. chrs.push(String.fromCharCode(j));
  95. }
  96. } else { // Supposed to treat period, begin and end as individual characters only, not a range
  97. chrs.push('.', rangeBegin, rangeEnd);
  98. }
  99. i += rangeEnd.length + 2; // Skip dots and range end (already skipped range end backslash if present)
  100. } else { // Character is by itself
  101. chrs.push(c);
  102. }
  103. }
  104. for (i = 0; i < str.length; i++) {
  105. c = str.charAt(i);
  106. if (chrs.indexOf(c) !== -1) {
  107. target += '\\';
  108. cca = c.charCodeAt(0);
  109. if (cca < 32 || cca > 126) { // Needs special escaping
  110. switch (c) {
  111. case '\n':
  112. target += 'n';
  113. break;
  114. case '\t':
  115. target += 't';
  116. break;
  117. case '\u000D':
  118. target += 'r';
  119. break;
  120. case '\u0007':
  121. target += 'a';
  122. break;
  123. case '\v':
  124. target += 'v';
  125. break;
  126. case '\b':
  127. target += 'b';
  128. break;
  129. case '\f':
  130. target += 'f';
  131. break;
  132. default:
  133. //target += _pad(cca.toString(8), 3);break; // Sufficient for UTF-16
  134. encoded = encodeURIComponent(c);
  135. // 3-length-padded UTF-8 octets
  136. if ((escHexGrp = percentHex.exec(encoded)) !== null) {
  137. target += _pad(parseInt(escHexGrp[1], 16)
  138. .toString(8), 3); // already added a slash above
  139. }
  140. while ((escHexGrp = percentHex.exec(encoded)) !== null) {
  141. target += '\\' + _pad(parseInt(escHexGrp[1], 16)
  142. .toString(8), 3);
  143. }
  144. break;
  145. }
  146. } else { // Perform regular backslashed escaping
  147. target += c;
  148. }
  149. } else { // Just add the character unescaped
  150. target += c;
  151. }
  152. }
  153. return target;
  154. }