SpecialString.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Generated by CoffeeScript 1.9.3
  2. var SpecialString, fn, i, len, prop, ref;
  3. module.exports = SpecialString = (function() {
  4. var self;
  5. self = SpecialString;
  6. SpecialString._tabRx = /^\t/;
  7. SpecialString._tagRx = /^<[^>]+>/;
  8. SpecialString._quotedHtmlRx = /^&(gt|lt|quot|amp|apos|sp);/;
  9. function SpecialString(str) {
  10. if (!(this instanceof self)) {
  11. return new self(str);
  12. }
  13. this._str = String(str);
  14. this._len = 0;
  15. }
  16. SpecialString.prototype._getStr = function() {
  17. return this._str;
  18. };
  19. SpecialString.prototype.set = function(str) {
  20. this._str = String(str);
  21. return this;
  22. };
  23. SpecialString.prototype.clone = function() {
  24. return new SpecialString(this._str);
  25. };
  26. SpecialString.prototype.isEmpty = function() {
  27. return this._str === '';
  28. };
  29. SpecialString.prototype.isOnlySpecialChars = function() {
  30. return !this.isEmpty() && this.length === 0;
  31. };
  32. SpecialString.prototype._reset = function() {
  33. return this._len = 0;
  34. };
  35. SpecialString.prototype.splitIn = function(limit, trimLeftEachLine) {
  36. var buffer, bufferLength, justSkippedSkipChar, lines;
  37. if (trimLeftEachLine == null) {
  38. trimLeftEachLine = false;
  39. }
  40. buffer = '';
  41. bufferLength = 0;
  42. lines = [];
  43. justSkippedSkipChar = false;
  44. self._countChars(this._str, function(char, charLength) {
  45. if (bufferLength > limit || bufferLength + charLength > limit) {
  46. lines.push(buffer);
  47. buffer = '';
  48. bufferLength = 0;
  49. }
  50. if (bufferLength === 0 && char === ' ' && !justSkippedSkipChar && trimLeftEachLine) {
  51. return justSkippedSkipChar = true;
  52. } else {
  53. buffer += char;
  54. bufferLength += charLength;
  55. return justSkippedSkipChar = false;
  56. }
  57. });
  58. if (buffer.length > 0) {
  59. lines.push(buffer);
  60. }
  61. return lines;
  62. };
  63. SpecialString.prototype.trim = function() {
  64. return new SpecialString(this.str.trim());
  65. };
  66. SpecialString.prototype.trimLeft = function() {
  67. return new SpecialString(this.str.replace(/^\s+/, ''));
  68. };
  69. SpecialString.prototype.trimRight = function() {
  70. return new SpecialString(this.str.replace(/\s+$/, ''));
  71. };
  72. SpecialString.prototype._getLength = function() {
  73. var sum;
  74. sum = 0;
  75. self._countChars(this._str, function(char, charLength) {
  76. sum += charLength;
  77. });
  78. return sum;
  79. };
  80. SpecialString.prototype.cut = function(from, to, trimLeft) {
  81. var after, before, cur, cut;
  82. if (trimLeft == null) {
  83. trimLeft = false;
  84. }
  85. if (to == null) {
  86. to = this.length;
  87. }
  88. from = parseInt(from);
  89. if (from >= to) {
  90. throw Error("`from` shouldn't be larger than `to`");
  91. }
  92. before = '';
  93. after = '';
  94. cut = '';
  95. cur = 0;
  96. self._countChars(this._str, (function(_this) {
  97. return function(char, charLength) {
  98. if (_this.str === 'ab<tag>') {
  99. console.log(charLength, char);
  100. }
  101. if (cur === from && char.match(/^\s+$/) && trimLeft) {
  102. return;
  103. }
  104. if (cur < from) {
  105. before += char;
  106. } else if (cur < to || cur + charLength <= to) {
  107. cut += char;
  108. } else {
  109. after += char;
  110. }
  111. cur += charLength;
  112. };
  113. })(this));
  114. this._str = before + after;
  115. this._reset();
  116. return SpecialString(cut);
  117. };
  118. SpecialString._countChars = function(text, cb) {
  119. var char, charLength, m;
  120. while (text.length !== 0) {
  121. if (m = text.match(self._tagRx)) {
  122. char = m[0];
  123. charLength = 0;
  124. text = text.substr(char.length, text.length);
  125. } else if (m = text.match(self._quotedHtmlRx)) {
  126. char = m[0];
  127. charLength = 1;
  128. text = text.substr(char.length, text.length);
  129. } else if (text.match(self._tabRx)) {
  130. char = "\t";
  131. charLength = 8;
  132. text = text.substr(1, text.length);
  133. } else {
  134. char = text[0];
  135. charLength = 1;
  136. text = text.substr(1, text.length);
  137. }
  138. cb.call(null, char, charLength);
  139. }
  140. };
  141. return SpecialString;
  142. })();
  143. ref = ['str', 'length'];
  144. fn = function() {
  145. var methodName;
  146. methodName = '_get' + prop[0].toUpperCase() + prop.substr(1, prop.length);
  147. return SpecialString.prototype.__defineGetter__(prop, function() {
  148. return this[methodName]();
  149. });
  150. };
  151. for (i = 0, len = ref.length; i < len; i++) {
  152. prop = ref[i];
  153. fn();
  154. }