TextUtils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C) 2013 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. WebInspector.TextUtils = {
  31. /**
  32. * @param {string} char
  33. * @return {boolean}
  34. */
  35. isStopChar: function(char)
  36. {
  37. return (char > " " && char < "0") ||
  38. (char > "9" && char < "A") ||
  39. (char > "Z" && char < "_") ||
  40. (char > "_" && char < "a") ||
  41. (char > "z" && char <= "~");
  42. },
  43. /**
  44. * @param {string} char
  45. * @return {boolean}
  46. */
  47. isWordChar: function(char)
  48. {
  49. return !WebInspector.TextUtils.isStopChar(char) && !WebInspector.TextUtils.isSpaceChar(char);
  50. },
  51. /**
  52. * @param {string} char
  53. * @return {boolean}
  54. */
  55. isSpaceChar: function(char)
  56. {
  57. return WebInspector.TextUtils._SpaceCharRegex.test(char);
  58. },
  59. /**
  60. * @param {string} word
  61. * @return {boolean}
  62. */
  63. isWord: function(word)
  64. {
  65. for (var i = 0; i < word.length; ++i) {
  66. if (!WebInspector.TextUtils.isWordChar(word.charAt(i)))
  67. return false;
  68. }
  69. return true;
  70. },
  71. /**
  72. * @param {string} char
  73. * @return {boolean}
  74. */
  75. isOpeningBraceChar: function(char)
  76. {
  77. return char === "(" || char === "{";
  78. },
  79. /**
  80. * @param {string} char
  81. * @return {boolean}
  82. */
  83. isClosingBraceChar: function(char)
  84. {
  85. return char === ")" || char === "}";
  86. },
  87. /**
  88. * @param {string} char
  89. * @return {boolean}
  90. */
  91. isBraceChar: function(char)
  92. {
  93. return WebInspector.TextUtils.isOpeningBraceChar(char) || WebInspector.TextUtils.isClosingBraceChar(char);
  94. },
  95. textToWords: function(text)
  96. {
  97. var words = [];
  98. var startWord = -1;
  99. for(var i = 0; i < text.length; ++i) {
  100. if (!WebInspector.TextUtils.isWordChar(text.charAt(i))) {
  101. if (startWord !== -1)
  102. words.push(text.substring(startWord, i));
  103. startWord = -1;
  104. } else if (startWord === -1)
  105. startWord = i;
  106. }
  107. if (startWord !== -1)
  108. words.push(text.substring(startWord));
  109. return words;
  110. },
  111. }
  112. WebInspector.TextUtils._SpaceCharRegex = /\s/;
  113. /**
  114. * @enum {string}
  115. */
  116. WebInspector.TextUtils.Indent = {
  117. TwoSpaces: " ",
  118. FourSpaces: " ",
  119. EightSpaces: " ",
  120. TabCharacter: "\t"
  121. }