本文整理汇总了TypeScript中buffer.Buffer.slice方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Buffer.slice方法的具体用法?TypeScript Buffer.slice怎么用?TypeScript Buffer.slice使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类buffer.Buffer
的用法示例。
在下文中一共展示了Buffer.slice方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: decodeString
function decodeString(data: Buffer, encoding?: string): DecodingResult {
let firstColonIndex = null;
for (let i = 0; i < data.length; i++) {
if (data[i] === Delimeters.colon) {
firstColonIndex = i;
break;
}
}
if (!firstColonIndex) {
throw new Error(`Error while decoding ${data.toString(encoding)}. Missing colon.`);
}
let expectedLength = parseInt(data.slice(0, firstColonIndex).toString());
let value = data.slice(firstColonIndex + 1, firstColonIndex + 1 + expectedLength);
return { value: value.toString(encoding), rest: data.slice(firstColonIndex + 1 + expectedLength) }
}
示例2: decodeInteger
function decodeInteger(data: Buffer): DecodingResult {
let endIndex: number = null // The first 'e' symbol index
for (let i = 1; i < data.length; i++) {
if (data[i] === Delimeters.e) {
endIndex = i;
break;
}
}
if (!endIndex) {
throw new Error("Invalid data. 'e' symbol expected");
}
return {
value: parseInt(data.slice(1, endIndex).toString()),
rest: data.slice(endIndex + 1)
}
}
示例3: decodeDict
function decodeDict(data: Buffer, encoding?: string): DecodingResult {
let result = {};
let rest = data.slice(1); // d...
let value = null;
let key: string;
while (rest.length !== 0) {
let firstByte = rest[0];
if (firstByte === Delimeters.e) {
rest = rest.slice(1);
break;
}
({ value: key, rest } = decodeString(rest, encoding));
({ value, rest } = _decode(rest, encoding));
result[key] = value;
}
return { value: result, rest: rest }
}
示例4: decodeList
function decodeList(data: Buffer, encoding?: string): DecodingResult {
// TODO: Check for 'e' symbol in the end.
let result = [];
let rest = data.slice(1); // l...
let value = null;
while (true) {
let firstByte = rest[0];
if (firstByte === Delimeters.i) {
({ value, rest } = decodeInteger(rest));
result.push(value);
continue;
}
if (encodesDigit(firstByte)) {
({ value, rest } = decodeString(rest, encoding));
result.push(value);
continue;
}
if (firstByte === Delimeters.l) {
({ value, rest } = decodeList(rest, encoding));
result.push(value);
continue;
}
if (firstByte === Delimeters.d) {
({ value, rest } = decodeDict(rest, encoding));
result.push(value);
continue;
}
if (firstByte === Delimeters.e) { // end of the list
rest = rest.slice(1);
break;
}
throw new Error(`Expected d, i, l or digit, got ${rest.toString(encoding)}`)
}
return { value: result, rest: rest };
}