1// Copyright 2022 The Pigweed Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); you may not 4// use this file except in compliance with the License. You may obtain a copy of 5// the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12// License for the specific language governing permissions and limitations under 13// the License. 14 15/** Parses CSV Database for easier lookups */ 16 17export class TokenDatabase { 18 private tokens: Map<number, string> = new Map(); 19 20 constructor(readonly csv: string) { 21 this.parseTokensToTokensMap(csv.split(/\r?\n/)); 22 } 23 24 has(token: number): boolean { 25 return this.tokens.has(token); 26 } 27 28 get(token: number): string | undefined { 29 return this.tokens.get(token); 30 } 31 32 private parseTokensToTokensMap(csv: string[]) { 33 for (const [lineNumber, line] of Object.entries( 34 csv.map((line) => line.split(/,/)), 35 )) { 36 if (!line[0] || !line[2]) { 37 continue; 38 } 39 if (!/^[a-fA-F0-9]+$/.test(line[0])) { 40 // Malformed number 41 console.error( 42 new Error( 43 `TokenDatabase number ${line[0]} at line ` + 44 `${lineNumber} is not a valid hex number`, 45 ), 46 ); 47 continue; 48 } 49 const tokenNumber = parseInt(line[0], 16); 50 // To extract actual string value of a token number, we: 51 // - Slice token number and whitespace that are in [0] and [1] of line. 52 // - Join the rest as a string and trim the trailing quotes. 53 const data = line.slice(2).join(',').slice(1, -1); 54 this.tokens.set(tokenNumber, data); 55 } 56 } 57} 58