@@ -230,6 +230,15 @@ public static int findFirstUnquotedChar(String line, char target) {
230
230
* @see #isInString(String, int)
231
231
*/
232
232
public static String [] splitByUnquotedString (String string , String separator ) {
233
+ if (splitByUnquotedCache == null ) splitByUnquotedCache = new HashMap <>();
234
+
235
+ // Check if the string and separator are already in the cache.
236
+ String [] splitByUnquotedArr = splitByUnquotedCache .get (Map .of (string , separator ));
237
+ if (splitByUnquotedArr != null ) {
238
+ return splitByUnquotedArr ;
239
+ }
240
+
241
+ // Otherwise, precompute and add result to the cache.
233
242
List <String > parts = new ArrayList <>();
234
243
int lastSplitIndex = 0 ;
235
244
@@ -251,8 +260,11 @@ public static String[] splitByUnquotedString(String string, String separator) {
251
260
parts .add (string .substring (lastSplitIndex ).trim ());
252
261
}
253
262
263
+ splitByUnquotedCache .put (Map .of (string , separator ), parts .toArray (new String [0 ]));
254
264
return parts .toArray (new String [0 ]);
255
265
}
266
+ private static Map <Map <String , String >, String []> splitByUnquotedCache ;
267
+ public static void clearSplitByUnquotedCache () { splitByUnquotedCache = null ; }
256
268
257
269
/**
258
270
* Splits the input string around occurrences of the given separator string, ignoring separators that appear inside
@@ -320,6 +332,15 @@ public static String[] splitByUnquotedString(String string, String separator, in
320
332
* @see #isInArray(String, int)
321
333
*/
322
334
public static String [] splitByStringNotInArray (String string , String separator ) {
335
+ if (splitByNotInArrayCache == null ) splitByNotInArrayCache = new HashMap <>();
336
+
337
+ // Check if the string and separator is already in the cache.
338
+ String [] splitByNotInArrayArr = splitByNotInArrayCache .get (Map .of (string , separator ));
339
+ if (splitByNotInArrayArr != null ) {
340
+ return splitByNotInArrayArr ;
341
+ }
342
+
343
+ // Otherwise, precompute and add result to the cache.
323
344
List <String > parts = new ArrayList <>();
324
345
int lastSplitIndex = 0 ;
325
346
@@ -341,10 +362,22 @@ public static String[] splitByStringNotInArray(String string, String separator)
341
362
parts .add (string .substring (lastSplitIndex ).trim ());
342
363
}
343
364
365
+ splitByNotInArrayCache .put (Map .of (string , separator ), parts .toArray (new String [0 ]));
344
366
return parts .toArray (new String [0 ]);
345
367
}
368
+ private static Map <Map <String , String >, String []> splitByNotInArrayCache ;
369
+ public static void clearSplitByNotInArrayCache () { splitByNotInArrayCache = null ; }
346
370
347
371
public static String [] splitByStringNotInObject (String string , String separator ) {
372
+ if (splitByNotInObjectCache == null ) splitByNotInObjectCache = new HashMap <>();
373
+
374
+ // Check if the string and separator are already in the cache.
375
+ String [] splitByNotInObjectArr = splitByNotInObjectCache .get (Map .of (string , separator ));
376
+ if (splitByNotInObjectArr != null ) {
377
+ return splitByNotInObjectArr ;
378
+ }
379
+
380
+ // Otherwise, precompute and add result to the cache.
348
381
List <String > parts = new ArrayList <>();
349
382
int lastSplitIndex = 0 ;
350
383
@@ -366,8 +399,11 @@ public static String[] splitByStringNotInObject(String string, String separator)
366
399
parts .add (string .substring (lastSplitIndex ).trim ());
367
400
}
368
401
402
+ splitByNotInObjectCache .put (Map .of (string , separator ), parts .toArray (new String [0 ]));
369
403
return parts .toArray (new String [0 ]);
370
404
}
405
+ private static Map <Map <String , String >, String []> splitByNotInObjectCache ;
406
+ public static void clearSplitByNotInObjectCache () { splitByNotInObjectCache = null ; }
371
407
372
408
/**
373
409
* Splits a string by a separator only when it is not inside an object or an array.
@@ -378,6 +414,15 @@ public static String[] splitByStringNotInObject(String string, String separator)
378
414
* @return
379
415
*/
380
416
public static String [] splitByStringNotNested (String string , String separator ) {
417
+ if (splitByNotNestedCache == null ) splitByNotNestedCache = new HashMap <>();
418
+
419
+ // Check if the string and separator are already in the cache.
420
+ String [] splitByNotNestedArr = splitByNotNestedCache .get (Map .of (string , separator ));
421
+ if (splitByNotNestedArr != null ) {
422
+ return splitByNotNestedArr ;
423
+ }
424
+
425
+ // Otherwise, precompute and add result to the cache.
381
426
List <String > parts = new ArrayList <>();
382
427
int lastSplitIndex = 0 ;
383
428
@@ -399,8 +444,11 @@ public static String[] splitByStringNotNested(String string, String separator) {
399
444
parts .add (string .substring (lastSplitIndex ).trim ());
400
445
}
401
446
447
+ splitByNotNestedCache .put (Map .of (string , separator ), parts .toArray (new String [0 ]));
402
448
return parts .toArray (new String [0 ]);
403
449
}
450
+ private static Map <Map <String , String >, String []> splitByNotNestedCache ;
451
+ public static void clearSplitByNotNestedCache () { splitByNotNestedCache = null ; }
404
452
405
453
/**
406
454
* Replaces the last occurrence of a specified regular expression with a replacement string in the input text.
0 commit comments