Skip to content

Commit

Permalink
improved string builder with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
odahcam committed Apr 16, 2021
1 parent 4c983ba commit 0af6bdd
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 14 deletions.
20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,26 @@
"./src/test/core/aztec/**/*.spec.ts"
],
"internalConsoleOptions": "openOnSessionStart"
},
{
"type": "node",
"request": "launch",
"name": "StringBuilder",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--require",
"ts-node/register",
"--require",
"tsconfig-paths/register",
"-u",
"tdd",
"--timeout",
"999999",
"--colors",
"--recursive",
"./src/test/core/util/StringBuilder.spec.ts"
],
"internalConsoleOptions": "openOnSessionStart"
}
]
}
38 changes: 24 additions & 14 deletions src/core/util/StringBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { char, int } from '../../customTypings';
import ArgumentException from '../ArgumentException';
import CharacterSetECI from '../common/CharacterSetECI';
import { int, char } from '../../customTypings';
import StringUtils from '../common/StringUtils';

export default class StringBuilder {
Expand All @@ -14,20 +15,29 @@ export default class StringBuilder {
}

public append(s: string | number): StringBuilder {
this.value += this.normalizeString(s);
return this;
}

public normalizeString(s: string | number) {
if (typeof s === 'string') {
this.value += s.toString();
} else if (this.encoding) {
return s.toString();
}

if (this.encoding) {
// use passed format (fromCharCode will return UTF8 encoding)
this.value += StringUtils.castAsNonUtf8Char(s, this.encoding);
} else {
// correctly converts from UTF-8, but not other encodings
this.value += String.fromCharCode(s);
return StringUtils.castAsNonUtf8Char(s, this.encoding);
}
return this;

// correctly converts from UTF-8, but not other encodings
return String.fromCharCode(s);
}

public appendChars(str: char[] | string[], offset: int, len: int): StringBuilder {
for (let i = offset; offset < offset + len; i++) {
const strLength = str.length;
if (strLength < len) throw new ArgumentException('`str` must be the same size or smaller than `len`');
for (let i = offset; i < offset + len; i++) {
if (i > strLength) throw new ArgumentException('Index out of bounds!');
this.append(str[i]);
}
return this;
Expand All @@ -53,9 +63,6 @@ export default class StringBuilder {
return this.value.substring(start, end);
}

/**
* @note helper method for RSS Expanded
*/
public setLengthToZero(): void {
this.value = '';
}
Expand All @@ -64,7 +71,10 @@ export default class StringBuilder {
return this.value;
}

public insert(n: number, c: string) {
this.value = this.value.substr(0, n) + c + this.value.substr(n + c.length);
public insert(n: number, s: string | number, replace: int = 0) {
const c = this.normalizeString(s);
const fromLength = !replace && replace !== 0 ? c.length : replace;
this.value = this.value.substr(0, n) + c + this.value.substr(n + fromLength);
return this;
}
}
134 changes: 134 additions & 0 deletions src/test/core/util/StringBuilder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@

import StringBuilder from '../../../core/util/StringBuilder';
import { assertEquals } from './AssertUtils';

describe('StringBuilder tests', () => {

it('initializes empty strings', () => {

const expected = '';
const sb = new StringBuilder();

assertEquals(sb.toString(), expected);
});

it('initializes strings', () => {

const expected = 'xyz';
const sb = new StringBuilder('xyz');

assertEquals(sb.toString(), expected);
});

it('appends strings', () => {

const expected1 = 'abcdef';
const sb1 = new StringBuilder();
const expected2 = '-abcdef';
const sb2 = new StringBuilder('-');

sb1.append('abc');
sb1.append('def');

sb2.append('abc');
sb2.append('def');

assertEquals(sb1.toString(), expected1);
assertEquals(sb2.toString(), expected2);
});

it('apends chars', () => {

const expected = '-&8xyzxy';
const sb = new StringBuilder('-&8');

sb.appendChars([120, 121, 122], 0, 1);
sb.appendChars([120, 121, 122], 1, 1);
sb.appendChars([120, 121, 122], 2, 1);
sb.appendChars([120, 121, 122], 0, 2);

assertEquals(sb.toString(), expected);
});

it('correctly normalizes UTF8 strings', () => {

const expected120 = 'x';
const input120 = 120;

const expected54 = '6';
const input54 = 54;

const expected80 = 'P';
const input80 = 80;

const expected37 = '%';
const input37 = 37;

const expected64 = '@';
const input64 = 64;

const sb = new StringBuilder();

const actual120 = sb.normalizeString(input120);
assertEquals(actual120, expected120);

const actual54 = sb.normalizeString(input54);
assertEquals(actual54, expected54);

const actual80 = sb.normalizeString(input80);
assertEquals(actual80, expected80);

const actual37 = sb.normalizeString(input37);
assertEquals(actual37, expected37);

const actual64 = sb.normalizeString(input64);
assertEquals(actual64, expected64);
});

it('correctly returns lentgh', () => {
const expected = 10;
const sb = new StringBuilder('----------');
assertEquals(sb.length(), expected);
});

it('sets lentgh to zero', () => {
const expected = 0;
const sb = new StringBuilder('----------');

sb.setLengthToZero();

assertEquals(sb.length(), expected);
});

it('returns the char at index', () => {
const expected = 'a';
const sb = new StringBuilder('----a----');
assertEquals(sb.charAt(4), expected);
});

it('changes the char at index', () => {
const expected = 'a';
const sb = new StringBuilder('---------');

sb.setCharAt(4, 'a');

assertEquals(sb.charAt(4), expected);
});

it('inserts and replaces chars', () => {
const expected1 = '----aaaa-----';
const expected2 = '----bbbb-----';
const expected3 = '----cccc-----';
const sb = new StringBuilder('---------');

sb.insert(4, 'aaaa');
assertEquals(sb.toString(), expected1);

sb.insert(4, 'bbbb', 4);
assertEquals(sb.toString(), expected2);

sb.insert(4, 'cccc', null);
assertEquals(sb.toString(), expected3);
});

});

0 comments on commit 0af6bdd

Please sign in to comment.