@@ -23,7 +23,7 @@ public DBDefinition Read(Stream stream, bool validate = false)
2323 if ( lines [ 0 ] . StartsWith ( "COLUMNS" ) )
2424 {
2525 lineNumber ++ ;
26- while ( lineNumber < lines . Count ( ) )
26+ while ( lineNumber < lines . Count )
2727 {
2828 var line = lines [ lineNumber ++ ] ;
2929
@@ -38,13 +38,13 @@ public DBDefinition Read(Stream stream, bool validate = false)
3838 var validTypes = new List < string > { "uint" , "int" , "float" , "string" , "locstring" } ;
3939
4040 // Check if line has a space in case someone didn't assign a type to a column name
41- if ( ! line . Contains ( " " ) )
41+ if ( ! line . Contains ( ' ' ) )
4242 {
4343 throw new Exception ( "Line " + line + " does not contain a space between type and column name!" ) ;
4444 }
4545
4646 // Read line up to space (end of type) or < (foreign key)
47- var type = line . Substring ( 0 , line . IndexOfAny ( new char [ ] { ' ' , '<' } ) ) ;
47+ var type = line [ .. line . IndexOfAny ( [ ' ' , '<' ] ) ] ;
4848
4949 // Check if type is valid, throw exception if not!
5050 if ( ! validTypes . Contains ( type ) )
@@ -61,7 +61,7 @@ public DBDefinition Read(Stream stream, bool validate = false)
6161 if ( line . StartsWith ( type + "<" ) )
6262 {
6363 // Read foreign key info between < and > without < and > in result, then split on :: to separate table and field
64- var foreignKey = line . Substring ( line . IndexOf ( '<' ) + 1 , line . IndexOf ( '>' ) - line . IndexOf ( '<' ) - 1 ) . Split ( new string [ ] { "::" } , StringSplitOptions . None ) ;
64+ var foreignKey = line . Substring ( line . IndexOf ( '<' ) + 1 , line . IndexOf ( '>' ) - line . IndexOf ( '<' ) - 1 ) . Split ( [ "::" ] , StringSplitOptions . None ) ;
6565
6666 // There should only be 2 values in foreignKey (table and col)
6767 if ( foreignKey . Length != 2 )
@@ -80,7 +80,7 @@ public DBDefinition Read(Stream stream, bool validate = false)
8080 // If there's only one space on the line at the same locaiton as the first one, assume a simple line like "uint ID", this can be better
8181 if ( line . LastIndexOf ( ' ' ) == line . IndexOf ( ' ' ) )
8282 {
83- name = line . Substring ( line . IndexOf ( ' ' ) + 1 ) ;
83+ name = line [ ( line . IndexOf ( ' ' ) + 1 ) .. ] ;
8484 }
8585 else
8686 {
@@ -94,10 +94,10 @@ public DBDefinition Read(Stream stream, bool validate = false)
9494 }
9595
9696 // If name ends in ? it's unverified
97- if ( name . EndsWith ( "?" ) )
97+ if ( name . EndsWith ( '?' ) )
9898 {
9999 columnDefinition . verified = false ;
100- name = name . Remove ( name . Length - 1 ) ;
100+ name = name [ .. ^ 1 ] ;
101101 }
102102 else
103103 {
@@ -107,20 +107,16 @@ public DBDefinition Read(Stream stream, bool validate = false)
107107 /* COMMENT READING */
108108 if ( line . Contains ( "//" ) )
109109 {
110- columnDefinition . comment = line . Substring ( line . IndexOf ( "//" ) + 2 ) . Trim ( ) ;
110+ columnDefinition . comment = line [ ( line . IndexOf ( "//" ) + 2 ) .. ] . Trim ( ) ;
111111 }
112112
113113 // Add to dictionary
114- if ( columnDefinitionDictionary . ContainsKey ( name ) )
114+ if ( ! columnDefinitionDictionary . TryAdd ( name , columnDefinition ) )
115115 {
116116 Console . ForegroundColor = ConsoleColor . Red ;
117117 Console . WriteLine ( "Collision with existing column name while adding new column name! Skipping.." ) ;
118118 Console . ResetColor ( ) ;
119119 }
120- else
121- {
122- columnDefinitionDictionary . Add ( name , columnDefinition ) ;
123- }
124120 }
125121 }
126122 else
@@ -144,41 +140,43 @@ public DBDefinition Read(Stream stream, bool validate = false)
144140
145141 if ( string . IsNullOrWhiteSpace ( line ) )
146142 {
147- if ( builds . Count != 0 || buildRanges . Count != 0 || layoutHashes . Count != 0 ) {
143+ if ( builds . Count != 0 || buildRanges . Count != 0 || layoutHashes . Count != 0 )
144+ {
148145 versionDefinitions . Add (
149146 new VersionDefinitions ( )
150147 {
151- builds = builds . ToArray ( ) ,
152- buildRanges = buildRanges . ToArray ( ) ,
153- layoutHashes = layoutHashes . ToArray ( ) ,
148+ builds = [ .. builds ] ,
149+ buildRanges = [ .. buildRanges ] ,
150+ layoutHashes = [ .. layoutHashes ] ,
154151 comment = comment ,
155- definitions = definitions . ToArray ( )
152+ definitions = [ .. definitions ]
156153 }
157154 ) ;
158155 }
159- else if ( definitions . Count != 0 || ! string . IsNullOrWhiteSpace ( comment ) ) {
156+ else if ( definitions . Count != 0 || ! string . IsNullOrWhiteSpace ( comment ) )
157+ {
160158 throw new Exception ( "No BUILD or LAYOUT, but non-empty lines/'definitions'." ) ;
161159 }
162160
163- definitions = new List < Definition > ( ) ;
164- layoutHashes = new List < string > ( ) ;
161+ definitions = [ ] ;
162+ layoutHashes = [ ] ;
165163 comment = "" ;
166- builds = new List < Build > ( ) ;
167- buildRanges = new List < BuildRange > ( ) ;
164+ builds = [ ] ;
165+ buildRanges = [ ] ;
168166 }
169167
170168 if ( line . StartsWith ( "LAYOUT" ) )
171169 {
172- var splitLayoutHashes = line . Remove ( 0 , 7 ) . Split ( new string [ ] { ", " } , StringSplitOptions . None ) ;
170+ var splitLayoutHashes = line [ 7 .. ] . Split ( [ ", " ] , StringSplitOptions . None ) ;
173171 layoutHashes . AddRange ( splitLayoutHashes ) ;
174172 }
175173
176174 if ( line . StartsWith ( "BUILD" ) )
177175 {
178- var splitBuilds = line . Remove ( 0 , 6 ) . Split ( new string [ ] { ", " } , StringSplitOptions . None ) ;
176+ var splitBuilds = line [ 6 .. ] . Split ( [ ", " ] , StringSplitOptions . None ) ;
179177 foreach ( var splitBuild in splitBuilds )
180178 {
181- if ( splitBuild . Contains ( "-" ) )
179+ if ( splitBuild . Contains ( '-' ) )
182180 {
183181 var splitRange = splitBuild . Split ( '-' ) ;
184182 buildRanges . Add (
@@ -195,20 +193,21 @@ public DBDefinition Read(Stream stream, bool validate = false)
195193
196194 if ( line . StartsWith ( "COMMENT" ) )
197195 {
198- comment = line . Substring ( 7 ) . Trim ( ) ;
196+ comment = line [ 7 .. ] . Trim ( ) ;
199197 }
200198
201199 if ( ! line . StartsWith ( "LAYOUT" ) && ! line . StartsWith ( "BUILD" ) && ! line . StartsWith ( "COMMENT" ) && ! string . IsNullOrWhiteSpace ( line ) )
202200 {
203- var definition = new Definition ( ) ;
204-
205- // Default to everything being inline
206- definition . isNonInline = false ;
201+ var definition = new Definition
202+ {
203+ // Default to everything being inline
204+ isNonInline = false
205+ } ;
207206
208- if ( line . Contains ( "$" ) )
207+ if ( line . Contains ( '$' ) )
209208 {
210- var annotationStart = line . IndexOf ( "$" ) ;
211- var annotationEnd = line . IndexOf ( "$" , 1 ) ;
209+ var annotationStart = line . IndexOf ( '$' ) ;
210+ var annotationEnd = line . IndexOf ( '$' , 1 ) ;
212211
213212 var annotations = new List < string > ( line . Substring ( annotationStart + 1 , annotationEnd - annotationStart - 1 ) . Split ( ',' ) ) ;
214213
@@ -230,7 +229,7 @@ public DBDefinition Read(Stream stream, bool validate = false)
230229 line = line . Remove ( annotationStart , annotationEnd + 1 ) ;
231230 }
232231
233- if ( line . Contains ( "<" ) )
232+ if ( line . Contains ( '<' ) )
234233 {
235234 var size = line . Substring ( line . IndexOf ( '<' ) + 1 , line . IndexOf ( '>' ) - line . IndexOf ( '<' ) - 1 ) ;
236235
@@ -248,16 +247,20 @@ public DBDefinition Read(Stream stream, bool validate = false)
248247 line = line . Remove ( line . IndexOf ( '<' ) , line . IndexOf ( '>' ) - line . IndexOf ( '<' ) + 1 ) ;
249248 }
250249
251- if ( line . Contains ( "[" ) )
250+ if ( line . Contains ( '[' ) )
252251 {
253- int . TryParse ( line . Substring ( line . IndexOf ( '[' ) + 1 , line . IndexOf ( ']' ) - line . IndexOf ( '[' ) - 1 ) , out definition . arrLength ) ;
252+ if ( ! int . TryParse ( line . AsSpan ( line . IndexOf ( '[' ) + 1 , line . IndexOf ( ']' ) - line . IndexOf ( '[' ) - 1 ) , out definition . arrLength ) )
253+ {
254+ throw new Exception ( "Invalid array length format." ) ;
255+ }
256+
254257 line = line . Remove ( line . IndexOf ( '[' ) , line . IndexOf ( ']' ) - line . IndexOf ( '[' ) + 1 ) ;
255258 }
256259
257260 if ( line . Contains ( "//" ) )
258261 {
259- definition . comment = line . Substring ( line . IndexOf ( "//" ) + 2 ) . Trim ( ) ;
260- line = line . Remove ( line . IndexOf ( "//" ) ) . Trim ( ) ;
262+ definition . comment = line [ ( line . IndexOf ( "//" ) + 2 ) .. ] . Trim ( ) ;
263+ line = line [ .. line . IndexOf ( "//" ) ] . Trim ( ) ;
261264 }
262265
263266 definition . name = line ;
@@ -281,19 +284,21 @@ public DBDefinition Read(Stream stream, bool validate = false)
281284
282285 if ( lines . Count == ( i + 1 ) )
283286 {
284- if ( builds . Count != 0 || buildRanges . Count != 0 || layoutHashes . Count != 0 ) {
287+ if ( builds . Count != 0 || buildRanges . Count != 0 || layoutHashes . Count != 0 )
288+ {
285289 versionDefinitions . Add (
286290 new VersionDefinitions ( )
287291 {
288- builds = builds . ToArray ( ) ,
289- buildRanges = buildRanges . ToArray ( ) ,
290- layoutHashes = layoutHashes . ToArray ( ) ,
292+ builds = [ .. builds ] ,
293+ buildRanges = [ .. buildRanges ] ,
294+ layoutHashes = [ .. layoutHashes ] ,
291295 comment = comment ,
292- definitions = definitions . ToArray ( )
296+ definitions = [ .. definitions ]
293297 }
294298 ) ;
295299 }
296- else if ( definitions . Count != 0 || ! string . IsNullOrWhiteSpace ( comment ) ) {
300+ else if ( definitions . Count != 0 || ! string . IsNullOrWhiteSpace ( comment ) )
301+ {
297302 throw new Exception ( "No BUILD or LAYOUT, but non-empty lines/'definitions'." ) ;
298303 }
299304 }
@@ -438,7 +443,7 @@ public DBDefinition Read(Stream stream, bool validate = false)
438443 return new DBDefinition
439444 {
440445 columnDefinitions = columnDefinitionDictionary ,
441- versionDefinitions = versionDefinitions . ToArray ( )
446+ versionDefinitions = [ .. versionDefinitions ]
442447 } ;
443448 }
444449
0 commit comments