| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 | /*  Copyright (c) 2007-2010 Alessandro Warth <awarth@cs.ucla.edu>  Permission is hereby granted, free of charge, to any person  obtaining a copy of this software and associated documentation  files (the "Software"), to deal in the Software without  restriction, including without limitation the rights to use,  copy, modify, merge, publish, distribute, sublicense, and/or sell  copies of the Software, and to permit persons to whom the  Software is furnished to do so, subject to the following  conditions:  The above copyright notice and this permission notice shall be  included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR  OTHER DEALINGS IN THE SOFTWARE.*/// try to use StringBuffer instead of string concatenation to improve performancefunction StringBuffer() {  this.strings = []  for (var idx = 0; idx < arguments.length; idx++)    this.nextPutAll(arguments[idx])}StringBuffer.prototype.nextPutAll = function(s) { this.strings.push(s) }StringBuffer.prototype.contents   = function()  { return this.strings.join("") }String.prototype.writeStream      = function() { return new StringBuffer(this) }// make Arrays print themselves sensiblyprintOn = function(x, ws) {  if (x === undefined || x === null)    ws.nextPutAll("" + x)  else if (x.constructor === Array) {    ws.nextPutAll("[")    for (var idx = 0; idx < x.length; idx++) {      if (idx > 0)        ws.nextPutAll(", ")      printOn(x[idx], ws)    }    ws.nextPutAll("]")  }  else    ws.nextPutAll(x.toString())}Array.prototype.toString = function() { var ws = "".writeStream(); printOn(this, ws); return ws.contents() }// delegationobjectThatDelegatesTo = function(x, props) {  var f = function() { }  f.prototype = x  var r = new f()  for (var p in props)    if (props.hasOwnProperty(p))      r[p] = props[p]  return r}// some reflective stuffownPropertyNames = function(x) {  var r = []  for (name in x)    if (x.hasOwnProperty(name))      r.push(name)  return r}isImmutable = function(x) {   return x === null || x === undefined || typeof x === "boolean" || typeof x === "number" || typeof x === "string"}String.prototype.digitValue  = function() { return this.charCodeAt(0) - "0".charCodeAt(0) }isSequenceable = function(x) { return typeof x == "string" || x.constructor === Array }// some functional programming stuffif(!Array.prototype.map) {  Array.prototype.map = function(f) {    var r = []    for (var idx = 0; idx < this.length; idx++)      r[idx] = f(this[idx])    return r  }}Array.prototype.reduce = function(f, z) {  var r = z  for (var idx = 0; idx < this.length; idx++)    r = f(r, this[idx])  return r}Array.prototype.delimWith = function(d) {  return this.reduce(    function(xs, x) {      if (xs.length > 0)        xs.push(d)      xs.push(x)      return xs    },   [])}// Squeak's ReadStream, kind offunction ReadStream(anArrayOrString) {  this.src = anArrayOrString  this.pos = 0}ReadStream.prototype.atEnd = function() { return this.pos >= this.src.length }ReadStream.prototype.next  = function() { return this.src.at(this.pos++) }// escape charactersescapeStringFor = new Object()for (var c = 0; c < 256; c++)  escapeStringFor[c] = String.fromCharCode(c)escapeStringFor["\\".charCodeAt(0)] = "\\\\"escapeStringFor['"'.charCodeAt(0)]  = '\\"'escapeStringFor["'".charCodeAt(0)]  = "\\'"escapeStringFor["\r".charCodeAt(0)] = "\\r"escapeStringFor["\n".charCodeAt(0)] = "\\n"escapeStringFor["\t".charCodeAt(0)] = "\\t"escapeChar = function(c) {  var charCode = c.charCodeAt(0)  return charCode > 255 ? String.fromCharCode(charCode) : escapeStringFor[charCode]}function unescape(s) {  if (s.charAt(0) == '\\')    switch (s.charAt(1)) {      case '\\': return '\\'      case 'r':  return '\r'      case 'n':  return '\n'      case 't':  return '\t'      default:   return s.charAt(1)    }  else    return s}String.prototype.toProgramString = function() {  var ws = "\"".writeStream()  for (var idx = 0; idx < this.length; idx++)    ws.nextPutAll(escapeChar(this.charAt(idx)))  ws.nextPutAll("\"")  return ws.contents()}// C-style tempnam functionfunction tempnam(s) { return (s ? s : "_tmpnam_") + tempnam.n++ }tempnam.n = 0// unique tags for objects (useful for making "hash tables")getTag = (function() {  var numIdx = 0  return function(x) {    if (x === null || x === undefined)      return x    switch (typeof x) {      case "boolean": return x == true ? "Btrue" : "Bfalse"      case "string":  return "S" + x      case "number":  return "N" + x      default:        return x.hasOwnProperty("_id_") ? x._id_ : x._id_ = "R" + numIdx++    }  }})()module.exports.StringBuffer = StringBuffer;
 |