diff --git a/src/index.spec.ts b/src/index.spec.ts index e962ffe..17a6a40 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -212,6 +212,18 @@ describe("parse(len, str)", () => { }); }); + it("should parse str with tab whitespace", () => { + var range = parse(1000, "bytes=\t40-80,\t81-90\t,-1\t"); + expect(range).toEqual({ + type: "bytes", + ranges: [ + { start: 40, end: 80 }, + { start: 81, end: 90 }, + { start: 999, end: 999 }, + ], + }); + }); + it("should parse non-byte range", () => { var range = parse(1000, "items=0-5"); expect(range).toEqual({ diff --git a/src/index.ts b/src/index.ts index 8640bea..d5b18f9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,8 +51,8 @@ export function parse(size: number, str: string): Result | -1 | -2 { // handle ows around numbers let startStrIndex = index; let endStrIndex = endIndex; - while (str.charCodeAt(startStrIndex) === 32) startStrIndex++; - while (str.charCodeAt(endStrIndex - 1) === 32) endStrIndex--; + while (isOws(str.charCodeAt(startStrIndex))) startStrIndex++; + while (isOws(str.charCodeAt(endStrIndex - 1))) endStrIndex--; let start = NaN; let end = NaN; @@ -103,6 +103,13 @@ export function parse(size: number, str: string): Result | -1 | -2 { return { type, ranges }; } +/** + * Check for optional whitespace (SP / HTAB), RFC 9110 section 5.6.3. + */ +function isOws(code: number): boolean { + return code === 32 || code === 9; +} + /** * Parse string to integer. */