Comparing version 0.9.19 to 0.9.20
{ | ||
"name": "bufrw", | ||
"version": "0.9.19", | ||
"version": "0.9.20", | ||
"description": "Buffer Reading and Writing", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -37,2 +37,7 @@ // Copyright (c) 2015 Uber Technologies, Inc. | ||
self.encoding = encoding || 'utf8'; | ||
// istanbul ignore else | ||
if (self.encoding === 'utf8') { | ||
self.writeInto = self.writeUtf8Into; | ||
self.readFrom = self.readUtf8From; | ||
} | ||
VariableBufferRW.call(self, sizerw); | ||
@@ -55,2 +60,3 @@ } | ||
// istanbul ignore next | ||
StringRW.prototype.writeInto = function writeInto(str, buffer, offset) { | ||
@@ -70,2 +76,22 @@ var self = this; | ||
StringRW.prototype.writeUtf8Into = function writeInto(str, buffer, offset) { | ||
var self = this; | ||
var start = offset + self.sizerw.width; | ||
var length = 0; | ||
if (typeof str === 'string') { | ||
// istanbul ignore else | ||
if (buffer.parent) { | ||
length = buffer.parent.utf8Write(str, buffer.offset + start, buffer.length - start); | ||
} else { | ||
length = buffer.write(str, start, self.encoding); | ||
} | ||
} else if (str !== null && str !== undefined) { | ||
return WriteResult.error(errors.expected(str, 'string, null, or undefined'), offset); | ||
} | ||
var res = self.sizerw.writeInto(length, buffer, offset); | ||
if (res.err) return res; | ||
return new WriteResult(null, start + length); | ||
}; | ||
// istanbul ignore next | ||
StringRW.prototype.readFrom = function readFrom(buffer, offset) { | ||
@@ -86,1 +112,22 @@ var self = this; | ||
}; | ||
StringRW.prototype.readUtf8From = function readFrom(buffer, offset) { | ||
var self = this; | ||
var res = self.sizerw.readFrom(buffer, offset); | ||
if (res.err) return res; | ||
var length = res.value; | ||
var remain = buffer.length - res.offset; | ||
if (remain < length) { | ||
return ReadResult.shortError(length, remain, offset, res.offset); | ||
} else { | ||
offset = res.offset; | ||
var end = offset + length; | ||
var str = ''; | ||
if (buffer.parent) { | ||
str = buffer.parent.utf8Slice(buffer.offset + offset, buffer.offset + end); | ||
} else { | ||
str = buffer.toString(self.encoding, offset, end); | ||
} | ||
return new ReadResult(null, end, str); | ||
} | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
163651
4081