Skip to content

Commit

Permalink
Merge pull request #2 from hypar-io/Performance
Browse files Browse the repository at this point in the history
make string splitting faster
  • Loading branch information
jamesbradleym authored Jun 21, 2024
2 parents ce2d6b9 + 765d976 commit 617fe61
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
2 changes: 2 additions & 0 deletions esm/DxfParser.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="node" />
import { Readable } from 'stream';
import IGeometry, { IEntity, IPoint } from './entities/geomtry.js';
export interface IBlock {
Expand Down Expand Up @@ -106,5 +107,6 @@ export default class DxfParser {
registerEntityHandler(handlerType: new () => IGeometry): void;
parseSync(source: string): IDxf | null;
parseStream(stream: Readable): Promise<IDxf>;
private _splitStringByNewline;
private _parse;
}
26 changes: 25 additions & 1 deletion esm/DxfParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,34 @@ export default class DxfParser {
});
});
}
_splitStringByNewline(str) {
const lines = [];
let currentIndex = 0;
let nextIndex;
// Split by \n
while ((nextIndex = str.indexOf('\n', currentIndex)) !== -1) {
let line = str.substring(currentIndex, nextIndex);
// Check if the line ends with a carriage return and remove it
if (line.endsWith('\r')) {
line = line.slice(0, -1); // Remove trailing \r if present (Windows-style)
}
lines.push(line);
currentIndex = nextIndex + 1;
}
if (currentIndex < str.length) {
let line = str.substring(currentIndex);
if (line.endsWith('\r')) {
line = line.slice(0, -1); // Remove trailing \r if present (Windows-style)
}
lines.push(line);
}
return lines;
}
;
_parse(dxfString) {
const dxf = {};
let lastHandle = 0;
const dxfLinesArray = dxfString.split(/\r\n|\r|\n/g);
const dxfLinesArray = this._splitStringByNewline(dxfString);
const scanner = new DxfArrayScanner(dxfLinesArray);
if (!scanner.hasNext())
throw Error('Empty file');
Expand Down
29 changes: 28 additions & 1 deletion src/DxfParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,37 @@ export default class DxfParser {
});
}

private _splitStringByNewline(str: string) {
const lines = [];
let currentIndex = 0;
let nextIndex;

// Split by \n
while ((nextIndex = str.indexOf('\n', currentIndex)) !== -1) {
let line = str.substring(currentIndex, nextIndex);
// Check if the line ends with a carriage return and remove it
if (line.endsWith('\r')) {
line = line.slice(0, -1); // Remove trailing \r if present (Windows-style)
}
lines.push(line);
currentIndex = nextIndex + 1;
}

if (currentIndex < str.length) {
let line = str.substring(currentIndex);
if (line.endsWith('\r')) {
line = line.slice(0, -1); // Remove trailing \r if present (Windows-style)
}
lines.push(line);
}

return lines;
};

private _parse(dxfString: string) {
const dxf = {} as IDxf;
let lastHandle = 0;
const dxfLinesArray = dxfString.split(/\r\n|\r|\n/g);
const dxfLinesArray = this._splitStringByNewline(dxfString);

const scanner = new DxfArrayScanner(dxfLinesArray);
if (!scanner.hasNext()) throw Error('Empty file');
Expand Down

0 comments on commit 617fe61

Please sign in to comment.