string-buf.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  4. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  5. // StringBuf - a way to keep string memory operations to a minimum
  6. // while building the strings for the xml files
  7. var StringBuf = /*#__PURE__*/function () {
  8. function StringBuf(options) {
  9. _classCallCheck(this, StringBuf);
  10. this._buf = Buffer.alloc(options && options.size || 16384);
  11. this._encoding = options && options.encoding || 'utf8'; // where in the buffer we are at
  12. this._inPos = 0; // for use by toBuffer()
  13. this._buffer = undefined;
  14. }
  15. _createClass(StringBuf, [{
  16. key: "toBuffer",
  17. value: function toBuffer() {
  18. // return the current data as a single enclosing buffer
  19. if (!this._buffer) {
  20. this._buffer = Buffer.alloc(this.length);
  21. this._buf.copy(this._buffer, 0, 0, this.length);
  22. }
  23. return this._buffer;
  24. }
  25. }, {
  26. key: "reset",
  27. value: function reset(position) {
  28. position = position || 0;
  29. this._buffer = undefined;
  30. this._inPos = position;
  31. }
  32. }, {
  33. key: "_grow",
  34. value: function _grow(min) {
  35. var size = this._buf.length * 2;
  36. while (size < min) {
  37. size *= 2;
  38. }
  39. var buf = Buffer.alloc(size);
  40. this._buf.copy(buf, 0);
  41. this._buf = buf;
  42. }
  43. }, {
  44. key: "addText",
  45. value: function addText(text) {
  46. this._buffer = undefined;
  47. var inPos = this._inPos + this._buf.write(text, this._inPos, this._encoding); // if we've hit (or nearing capacity), grow the buf
  48. while (inPos >= this._buf.length - 4) {
  49. this._grow(this._inPos + text.length); // keep trying to write until we've completely written the text
  50. inPos = this._inPos + this._buf.write(text, this._inPos, this._encoding);
  51. }
  52. this._inPos = inPos;
  53. }
  54. }, {
  55. key: "addStringBuf",
  56. value: function addStringBuf(inBuf) {
  57. if (inBuf.length) {
  58. this._buffer = undefined;
  59. if (this.length + inBuf.length > this.capacity) {
  60. this._grow(this.length + inBuf.length);
  61. } // eslint-disable-next-line no-underscore-dangle
  62. inBuf._buf.copy(this._buf, this._inPos, 0, inBuf.length);
  63. this._inPos += inBuf.length;
  64. }
  65. }
  66. }, {
  67. key: "length",
  68. get: function get() {
  69. return this._inPos;
  70. }
  71. }, {
  72. key: "capacity",
  73. get: function get() {
  74. return this._buf.length;
  75. }
  76. }, {
  77. key: "buffer",
  78. get: function get() {
  79. return this._buf;
  80. }
  81. }]);
  82. return StringBuf;
  83. }();
  84. module.exports = StringBuf;
  85. //# sourceMappingURL=string-buf.js.map