Skip to content

Commit

Permalink
Displaystring work.
Browse files Browse the repository at this point in the history
Currently very incomplete, but tests are running.
  • Loading branch information
evert committed Jan 28, 2024
1 parent ce18ef5 commit ede5f94
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/displaystring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export class DisplayString {

private value: string;
constructor(value: string) {

this.value = value;

}

toString(): string {

return this.value;

}

}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './parser';
export * from './types';
export * from './util';
export { Token } from './token';
export { DisplayString } from './displaystring';
30 changes: 30 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { Token } from './token';

import { isAscii } from './util';
import { DisplayString } from './displaystring';

export function parseDictionary(input: string): Dictionary {

Expand Down Expand Up @@ -185,6 +186,9 @@ export default class Parser {
if (char === '@') {
return this.parseDate();
}
if (char === '%') {
return this.parseDisplayString();
}
throw new ParseError(this.pos, 'Unexpected input');

}
Expand Down Expand Up @@ -299,6 +303,32 @@ export default class Parser {

}

private parseDisplayString(): DisplayString {

this.expectChar('%');
this.pos++;
this.expectChar('"');
this.pos++;

let result = new Uint8Array();

while (!this.eof()) {

const char = this.getChar();
if (char.charCodeAt(0) <= 0x1F || (char.charCodeAt(0) >= 0x7F && char.charCodeAt(0) <= 0xFF)) {
throw new ParseError('Invalid byte found at offset: ' + this.pos);
}

if (char==='%') {
const hexChars = this.input.substr(this.pos,2);
if (/^[0-9a-f]{2}$/.test(hexChars)) {

}

return new DisplayString(result);

}

private parseToken(): Token {

Check failure on line 332 in src/parser.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

Declaration or statement expected.

Check failure on line 332 in src/parser.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Declaration or statement expected.

Check failure on line 332 in src/parser.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Declaration or statement expected.

Check failure on line 332 in src/parser.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Declaration or statement expected.

// The specification wants this check, but it's an unreachable code block.
Expand Down
8 changes: 8 additions & 0 deletions src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { Token } from './token';

import { isAscii, isInnerList, isValidKeyStr } from './util';
import { DisplayString } from './displaystring';

export class SerializeError extends Error {}

Expand Down Expand Up @@ -80,6 +81,9 @@ export function serializeBareItem(input: BareItem): string {
if (input instanceof ByteSequence) {
return serializeByteSequence(input);
}
if (input instanceof DisplayString) {
return serializeDisplayString(input);
}
if (input instanceof Date) {
return serializeDate(input);
}
Expand Down Expand Up @@ -114,6 +118,10 @@ export function serializeString(input: string): string {
return `"${input.replace(/("|\\)/g, (v) => '\\' + v)}"`;
}

export function serializeDisplayString(input: DisplayString): string {
return '%' + serializeString(input.toString());
}

export function serializeBoolean(input: boolean): string {
return input ? '?1' : '?0';
}
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Token } from './token';
import { DisplayString } from './displaystring';

/**
* Lists are arrays of zero or more members, each of which can be an Item
Expand Down Expand Up @@ -47,6 +48,6 @@ export class ByteSequence {

}

export type BareItem = number | string | Token | ByteSequence | Date | boolean;
export type BareItem = number | string | Token | ByteSequence | Date | boolean | DisplayString;

export type Item = [BareItem, Parameters];
11 changes: 10 additions & 1 deletion test/httpwg-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {

ParseError,
} = require('../dist');
const { Token, ByteSequence } = require('../dist');
const { Token, ByteSequence, DisplayString } = require('../dist');
const base32Encode = require('base32-encode');
const base32Decode = require('base32-decode');
const fs = require('fs');
Expand All @@ -24,6 +24,7 @@ describe('HTTP-WG tests', () => {
'string',
'token',
'date',
'display-string',

'item',

Expand Down Expand Up @@ -284,6 +285,12 @@ function packTestValue(input) {
value: input.toString()
}
}
if(input instanceof DisplayString) {
return {
__type: 'displaystring',
value: input.toString()
}
}
if (input instanceof ByteSequence) {
return {
__type: 'binary',
Expand Down Expand Up @@ -340,6 +347,8 @@ function unpackTestValue(input) {
return new ByteSequence(Buffer.from(base32Decode(input.value, 'RFC4648')).toString('base64'));
case 'date' :
return new Date(input.value * 1000);
case 'displaystring' :
return new DisplayString(input.value);
default:
throw new Error('Unknown input __type: ' + input.__type);
}
Expand Down

0 comments on commit ede5f94

Please sign in to comment.