lib.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. Copyright (c) 2007-2010 Alessandro Warth <awarth@cs.ucla.edu>
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. // try to use StringBuffer instead of string concatenation to improve performance
  23. function StringBuffer() {
  24. this.strings = []
  25. for (var idx = 0; idx < arguments.length; idx++)
  26. this.nextPutAll(arguments[idx])
  27. }
  28. StringBuffer.prototype.nextPutAll = function(s) { this.strings.push(s) }
  29. StringBuffer.prototype.contents = function() { return this.strings.join("") }
  30. String.prototype.writeStream = function() { return new StringBuffer(this) }
  31. // make Arrays print themselves sensibly
  32. printOn = function(x, ws) {
  33. if (x === undefined || x === null)
  34. ws.nextPutAll("" + x)
  35. else if (x.constructor === Array) {
  36. ws.nextPutAll("[")
  37. for (var idx = 0; idx < x.length; idx++) {
  38. if (idx > 0)
  39. ws.nextPutAll(", ")
  40. printOn(x[idx], ws)
  41. }
  42. ws.nextPutAll("]")
  43. }
  44. else
  45. ws.nextPutAll(x.toString())
  46. }
  47. Array.prototype.toString = function() { var ws = "".writeStream(); printOn(this, ws); return ws.contents() }
  48. // delegation
  49. objectThatDelegatesTo = function(x, props) {
  50. var f = function() { }
  51. f.prototype = x
  52. var r = new f()
  53. for (var p in props)
  54. if (props.hasOwnProperty(p))
  55. r[p] = props[p]
  56. return r
  57. }
  58. // some reflective stuff
  59. ownPropertyNames = function(x) {
  60. var r = []
  61. for (name in x)
  62. if (x.hasOwnProperty(name))
  63. r.push(name)
  64. return r
  65. }
  66. isImmutable = function(x) {
  67. return x === null || x === undefined || typeof x === "boolean" || typeof x === "number" || typeof x === "string"
  68. }
  69. String.prototype.digitValue = function() { return this.charCodeAt(0) - "0".charCodeAt(0) }
  70. isSequenceable = function(x) { return typeof x == "string" || x.constructor === Array }
  71. // some functional programming stuff
  72. if(!Array.prototype.map) {
  73. Array.prototype.map = function(f) {
  74. var r = []
  75. for (var idx = 0; idx < this.length; idx++)
  76. r[idx] = f(this[idx])
  77. return r
  78. }
  79. }
  80. Array.prototype.reduce = function(f, z) {
  81. var r = z
  82. for (var idx = 0; idx < this.length; idx++)
  83. r = f(r, this[idx])
  84. return r
  85. }
  86. Array.prototype.delimWith = function(d) {
  87. return this.reduce(
  88. function(xs, x) {
  89. if (xs.length > 0)
  90. xs.push(d)
  91. xs.push(x)
  92. return xs
  93. },
  94. [])
  95. }
  96. // Squeak's ReadStream, kind of
  97. function ReadStream(anArrayOrString) {
  98. this.src = anArrayOrString
  99. this.pos = 0
  100. }
  101. ReadStream.prototype.atEnd = function() { return this.pos >= this.src.length }
  102. ReadStream.prototype.next = function() { return this.src.at(this.pos++) }
  103. // escape characters
  104. escapeStringFor = new Object()
  105. for (var c = 0; c < 256; c++)
  106. escapeStringFor[c] = String.fromCharCode(c)
  107. escapeStringFor["\\".charCodeAt(0)] = "\\\\"
  108. escapeStringFor['"'.charCodeAt(0)] = '\\"'
  109. escapeStringFor["'".charCodeAt(0)] = "\\'"
  110. escapeStringFor["\r".charCodeAt(0)] = "\\r"
  111. escapeStringFor["\n".charCodeAt(0)] = "\\n"
  112. escapeStringFor["\t".charCodeAt(0)] = "\\t"
  113. escapeChar = function(c) {
  114. var charCode = c.charCodeAt(0)
  115. return charCode > 255 ? String.fromCharCode(charCode) : escapeStringFor[charCode]
  116. }
  117. function unescape(s) {
  118. if (s.charAt(0) == '\\')
  119. switch (s.charAt(1)) {
  120. case '\\': return '\\'
  121. case 'r': return '\r'
  122. case 'n': return '\n'
  123. case 't': return '\t'
  124. default: return s.charAt(1)
  125. }
  126. else
  127. return s
  128. }
  129. String.prototype.toProgramString = function() {
  130. var ws = "\"".writeStream()
  131. for (var idx = 0; idx < this.length; idx++)
  132. ws.nextPutAll(escapeChar(this.charAt(idx)))
  133. ws.nextPutAll("\"")
  134. return ws.contents()
  135. }
  136. // C-style tempnam function
  137. function tempnam(s) { return (s ? s : "_tmpnam_") + tempnam.n++ }
  138. tempnam.n = 0
  139. // unique tags for objects (useful for making "hash tables")
  140. getTag = (function() {
  141. var numIdx = 0
  142. return function(x) {
  143. if (x === null || x === undefined)
  144. return x
  145. switch (typeof x) {
  146. case "boolean": return x == true ? "Btrue" : "Bfalse"
  147. case "string": return "S" + x
  148. case "number": return "N" + x
  149. default: return x.hasOwnProperty("_id_") ? x._id_ : x._id_ = "R" + numIdx++
  150. }
  151. }
  152. })()
  153. module.exports.StringBuffer = StringBuffer;