File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
arduino-core/src/processing/app/helpers Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 2424import java .util .ArrayList ;
2525import java .util .List ;
2626import java .util .Map ;
27+ import java .util .UUID ;
2728
2829public class StringReplacer {
2930
31+ public static void checkIfRequiredKeyIsMissingOrExcept (String key , String src , PreferencesMap inDict ) throws PreferencesMapException {
32+ // If the key is not missing -> everything is OK
33+ String checkedValue = inDict .get (key );
34+ if (checkedValue != null && !checkedValue .isEmpty ())
35+ return ;
36+
37+ PreferencesMap dict = new PreferencesMap (inDict );
38+
39+ // Find a random tag that is not contained in the dictionary and the src pattern
40+ String tag ;
41+ while (true ) {
42+ tag = UUID .randomUUID ().toString ();
43+ if (src .contains (tag ))
44+ continue ;
45+ if (dict .values ().contains (tag ))
46+ continue ;
47+ if (dict .keySet ().contains (tag ))
48+ continue ;
49+ break ;
50+ }
51+
52+ // Inject tag inside the dictionary
53+ dict .put (key , tag );
54+
55+ // Recursive replace with a max depth of 10 levels.
56+ String res ;
57+ for (int i = 0 ; i < 10 ; i ++) {
58+ // Do a replace with dictionary
59+ res = StringReplacer .replaceFromMapping (src , dict );
60+ if (res .equals (src ))
61+ break ;
62+ src = res ;
63+ }
64+
65+ // If the resulting string contains the tag, then the key is required
66+ if (src .contains (tag )) {
67+ throw new PreferencesMapException (key );
68+ }
69+ }
70+
3071 public static String [] formatAndSplit (String src , Map <String , String > dict ) throws Exception {
3172 String res ;
3273
You can’t perform that action at this time.
0 commit comments