File tree Expand file tree Collapse file tree 3 files changed +55
-2
lines changed Expand file tree Collapse file tree 3 files changed +55
-2
lines changed Original file line number Diff line number Diff line change
1
+ /// <reference types="node" />
1
2
import { Readable } from 'stream' ;
2
3
import IGeometry , { IEntity , IPoint } from './entities/geomtry.js' ;
3
4
export interface IBlock {
@@ -106,5 +107,6 @@ export default class DxfParser {
106
107
registerEntityHandler ( handlerType : new ( ) => IGeometry ) : void ;
107
108
parseSync ( source : string ) : IDxf | null ;
108
109
parseStream ( stream : Readable ) : Promise < IDxf > ;
110
+ private _splitStringByNewline ;
109
111
private _parse ;
110
112
}
Original file line number Diff line number Diff line change @@ -84,10 +84,34 @@ export default class DxfParser {
84
84
} ) ;
85
85
} ) ;
86
86
}
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
+ ;
87
111
_parse ( dxfString ) {
88
112
const dxf = { } ;
89
113
let lastHandle = 0 ;
90
- const dxfLinesArray = dxfString . split ( / \r \n | \r | \n / g ) ;
114
+ const dxfLinesArray = this . _splitStringByNewline ( dxfString ) ;
91
115
const scanner = new DxfArrayScanner ( dxfLinesArray ) ;
92
116
if ( ! scanner . hasNext ( ) )
93
117
throw Error ( 'Empty file' ) ;
Original file line number Diff line number Diff line change @@ -212,10 +212,37 @@ export default class DxfParser {
212
212
} ) ;
213
213
}
214
214
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
+
215
242
private _parse ( dxfString : string ) {
216
243
const dxf = { } as IDxf ;
217
244
let lastHandle = 0 ;
218
- const dxfLinesArray = dxfString . split ( / \r \n | \r | \n / g ) ;
245
+ const dxfLinesArray = this . _splitStringByNewline ( dxfString ) ;
219
246
220
247
const scanner = new DxfArrayScanner ( dxfLinesArray ) ;
221
248
if ( ! scanner . hasNext ( ) ) throw Error ( 'Empty file' ) ;
You can’t perform that action at this time.
0 commit comments