Skip to content

Commit

Permalink
Expose fine-parsing functions as utils
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan818fr committed Feb 3, 2022
1 parent 6c87d59 commit 149042d
Show file tree
Hide file tree
Showing 4 changed files with 3,310 additions and 3,272 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@types/jest": "^24.0.18",
"@types/node": "^12.7.11",
"jest": "^24.9.0",
"prettier": "^2.5.1",
"rimraf": "^3.0.2",
"ts-jest": "^24.3.0",
"tslint": "^5.20.1",
Expand Down
49 changes: 5 additions & 44 deletions src/java-props.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {decodeLine, encodeLine, LineReader} from './utils';
import {decodeLine, encodeLine, rawParse} from './utils';

export interface Properties {
[key: string]: string;
Expand All @@ -18,49 +18,10 @@ export interface Properties {
*/
export function parse(str: string): Properties {
const result: Properties = Object.create(null);
const lr = new LineReader(str);
let line;
while ((line = lr.readLine()) !== undefined) {
let keyLen = 0;
let valueStart = line.length;
let hasSep = false;
let backslash = false;

const lineLen = line.length;
let pos = 0;
for (; pos < lineLen; pos++) {
const c = line[pos];
if ((c === '=' || c === ':') && !backslash) {
valueStart = keyLen + 1;
hasSep = true;
break;
} else if ((c === ' ' || c === '\t' || c === '\f') && !backslash) {
valueStart = keyLen + 1;
break;
}
if (c === '\\') {
backslash = !backslash;
} else {
backslash = false;
}
keyLen++;
}
while (valueStart < lineLen) {
const c = line[valueStart];
if (c !== ' ' && c !== '\t' && c !== '\f') {
if (!hasSep && (c === '=' || c === ':')) {
hasSep = true;
} else {
break;
}
}
valueStart++;
}

const key = decodeLine(line.substring(0, keyLen));
const value = decodeLine(line.substring(valueStart));
result[key] = value;
}
rawParse(str, (res) => {
const key = decodeLine(res.line.substring(0, res.sepStart));
result[key] = decodeLine(res.line.substring(res.valueStart));
});
return result;
}

Expand Down
60 changes: 57 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,60 @@ const UNICODE_PATTERN = /^[0-9a-fA-F]{4}$/;
const ENCODE_PATTERN = /(?:[\u0000-\u001F\\\u007F-\uFFFF])/g;
const ENCODE_KEY_PATTERN = /(?:[\u0000-\u0020!#:=\\\u007F-\uFFFF])/g; // ENCODE_PATTERN with separators + comments

export interface ParsedLine {
line: string;
sepStart: number;
valueStart: number;
}

export function rawParse(str: string, consumeFn: (res: ParsedLine) => void): void {
const lr = new LineReader(str);
let line;
while ((line = lr.readLine()) !== undefined) {
consumeFn(rawParseLine(line));
}
}

export function rawParseLine(line: string): ParsedLine {
let keyLen = 0;
let valueStart = line.length;
let hasSep = false;
let backslash = false;

const lineLen = line.length;
let pos = 0;
for (; pos < lineLen; pos++) {
const c = line[pos];
if ((c === '=' || c === ':') && !backslash) {
valueStart = keyLen + 1;
hasSep = true;
break;
} else if ((c === ' ' || c === '\t' || c === '\f') && !backslash) {
valueStart = keyLen + 1;
break;
}
if (c === '\\') {
backslash = !backslash;
} else {
backslash = false;
}
keyLen++;
}
while (valueStart < lineLen) {
const c = line[valueStart];
if (c !== ' ' && c !== '\t' && c !== '\f') {
if (!hasSep && (c === '=' || c === ':')) {
hasSep = true;
} else {
break;
}
}
valueStart++;
}

return {line, sepStart: keyLen, valueStart};
}

export function decodeLine(line: string): string {
return line.replace(DECODE_PATTERN, (_, unicode, char) => {
if (unicode !== undefined) {
Expand Down Expand Up @@ -61,9 +115,9 @@ export function encodeLine(line: string, isKey?: boolean): string {
export const convertLine = decodeLine;

export class LineReader {
private readonly str: string;
private readonly strLen: number;
private pos = 0;
public readonly str: string;
public readonly strLen: number;
public pos = 0;

public constructor(str: string) {
this.str = str;
Expand Down
Loading

0 comments on commit 149042d

Please sign in to comment.