strings.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. (function (factory) {
  6. if (typeof module === "object" && typeof module.exports === "object") {
  7. var v = factory(require, exports);
  8. if (v !== undefined) module.exports = v;
  9. }
  10. else if (typeof define === "function" && define.amd) {
  11. define(["require", "exports"], factory);
  12. }
  13. })(function (require, exports) {
  14. "use strict";
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.extendedRegExp = exports.repeat = exports.convertSimple2RegExpPattern = exports.endsWith = exports.startsWith = void 0;
  17. function startsWith(haystack, needle) {
  18. if (haystack.length < needle.length) {
  19. return false;
  20. }
  21. for (var i = 0; i < needle.length; i++) {
  22. if (haystack[i] !== needle[i]) {
  23. return false;
  24. }
  25. }
  26. return true;
  27. }
  28. exports.startsWith = startsWith;
  29. /**
  30. * Determines if haystack ends with needle.
  31. */
  32. function endsWith(haystack, needle) {
  33. var diff = haystack.length - needle.length;
  34. if (diff > 0) {
  35. return haystack.lastIndexOf(needle) === diff;
  36. }
  37. else if (diff === 0) {
  38. return haystack === needle;
  39. }
  40. else {
  41. return false;
  42. }
  43. }
  44. exports.endsWith = endsWith;
  45. function convertSimple2RegExpPattern(pattern) {
  46. return pattern.replace(/[\-\\\{\}\+\?\|\^\$\.\,\[\]\(\)\#\s]/g, '\\$&').replace(/[\*]/g, '.*');
  47. }
  48. exports.convertSimple2RegExpPattern = convertSimple2RegExpPattern;
  49. function repeat(value, count) {
  50. var s = '';
  51. while (count > 0) {
  52. if ((count & 1) === 1) {
  53. s += value;
  54. }
  55. value += value;
  56. count = count >>> 1;
  57. }
  58. return s;
  59. }
  60. exports.repeat = repeat;
  61. function extendedRegExp(pattern) {
  62. if (startsWith(pattern, '(?i)')) {
  63. return new RegExp(pattern.substring(4), 'i');
  64. }
  65. else {
  66. return new RegExp(pattern);
  67. }
  68. }
  69. exports.extendedRegExp = extendedRegExp;
  70. });