strings.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. export function startsWith(haystack, needle) {
  6. if (haystack.length < needle.length) {
  7. return false;
  8. }
  9. for (var i = 0; i < needle.length; i++) {
  10. if (haystack[i] !== needle[i]) {
  11. return false;
  12. }
  13. }
  14. return true;
  15. }
  16. /**
  17. * Determines if haystack ends with needle.
  18. */
  19. export function endsWith(haystack, needle) {
  20. var diff = haystack.length - needle.length;
  21. if (diff > 0) {
  22. return haystack.lastIndexOf(needle) === diff;
  23. }
  24. else if (diff === 0) {
  25. return haystack === needle;
  26. }
  27. else {
  28. return false;
  29. }
  30. }
  31. export function convertSimple2RegExpPattern(pattern) {
  32. return pattern.replace(/[\-\\\{\}\+\?\|\^\$\.\,\[\]\(\)\#\s]/g, '\\$&').replace(/[\*]/g, '.*');
  33. }
  34. export function repeat(value, count) {
  35. var s = '';
  36. while (count > 0) {
  37. if ((count & 1) === 1) {
  38. s += value;
  39. }
  40. value += value;
  41. count = count >>> 1;
  42. }
  43. return s;
  44. }
  45. export function extendedRegExp(pattern) {
  46. if (startsWith(pattern, '(?i)')) {
  47. return new RegExp(pattern.substring(4), 'i');
  48. }
  49. else {
  50. return new RegExp(pattern);
  51. }
  52. }