Skip to content

Commit 617fe61

Browse files
Merge pull request #2 from hypar-io/Performance
make string splitting faster
2 parents ce2d6b9 + 765d976 commit 617fe61

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

esm/DxfParser.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference types="node" />
12
import { Readable } from 'stream';
23
import IGeometry, { IEntity, IPoint } from './entities/geomtry.js';
34
export interface IBlock {
@@ -106,5 +107,6 @@ export default class DxfParser {
106107
registerEntityHandler(handlerType: new () => IGeometry): void;
107108
parseSync(source: string): IDxf | null;
108109
parseStream(stream: Readable): Promise<IDxf>;
110+
private _splitStringByNewline;
109111
private _parse;
110112
}

esm/DxfParser.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,34 @@ export default class DxfParser {
8484
});
8585
});
8686
}
87+
_splitStringByNewline(str) {
88+
const lines = [];
89+
let currentIndex = 0;
90+
let nextIndex;
91+
// Split by \n
92+
while ((nextIndex = str.indexOf('\n', currentIndex)) !== -1) {
93+
let line = str.substring(currentIndex, nextIndex);
94+
// Check if the line ends with a carriage return and remove it
95+
if (line.endsWith('\r')) {
96+
line = line.slice(0, -1); // Remove trailing \r if present (Windows-style)
97+
}
98+
lines.push(line);
99+
currentIndex = nextIndex + 1;
100+
}
101+
if (currentIndex < str.length) {
102+
let line = str.substring(currentIndex);
103+
if (line.endsWith('\r')) {
104+
line = line.slice(0, -1); // Remove trailing \r if present (Windows-style)
105+
}
106+
lines.push(line);
107+
}
108+
return lines;
109+
}
110+
;
87111
_parse(dxfString) {
88112
const dxf = {};
89113
let lastHandle = 0;
90-
const dxfLinesArray = dxfString.split(/\r\n|\r|\n/g);
114+
const dxfLinesArray = this._splitStringByNewline(dxfString);
91115
const scanner = new DxfArrayScanner(dxfLinesArray);
92116
if (!scanner.hasNext())
93117
throw Error('Empty file');

src/DxfParser.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,37 @@ export default class DxfParser {
212212
});
213213
}
214214

215+
private _splitStringByNewline(str: string) {
216+
const lines = [];
217+
let currentIndex = 0;
218+
let nextIndex;
219+
220+
// Split by \n
221+
while ((nextIndex = str.indexOf('\n', currentIndex)) !== -1) {
222+
let line = str.substring(currentIndex, nextIndex);
223+
// Check if the line ends with a carriage return and remove it
224+
if (line.endsWith('\r')) {
225+
line = line.slice(0, -1); // Remove trailing \r if present (Windows-style)
226+
}
227+
lines.push(line);
228+
currentIndex = nextIndex + 1;
229+
}
230+
231+
if (currentIndex < str.length) {
232+
let line = str.substring(currentIndex);
233+
if (line.endsWith('\r')) {
234+
line = line.slice(0, -1); // Remove trailing \r if present (Windows-style)
235+
}
236+
lines.push(line);
237+
}
238+
239+
return lines;
240+
};
241+
215242
private _parse(dxfString: string) {
216243
const dxf = {} as IDxf;
217244
let lastHandle = 0;
218-
const dxfLinesArray = dxfString.split(/\r\n|\r|\n/g);
245+
const dxfLinesArray = this._splitStringByNewline(dxfString);
219246

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

0 commit comments

Comments
 (0)