Skip to content

Commit

Permalink
Added missing parseIntArray2D(..) method
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephane-D committed Nov 7, 2024
1 parent 35d7178 commit 00ea079
Showing 1 changed file with 79 additions and 6 deletions.
85 changes: 79 additions & 6 deletions tools/commons/src/sgdk/tool/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public static boolean isEmpty(String value)
}

/**
* Try to parse a boolean from the specified String and return it.
* Try to parse a boolean from the specified String and returns it.
* Return 'def' is we can't parse any boolean from the string.
*/
public static boolean parseBoolean(String s, boolean def)
Expand All @@ -417,7 +417,7 @@ public static boolean parseBoolean(String s, boolean def)
}

/**
* Try to parse a integer from the specified String and return it.
* Try to parse a integer from the specified String and returns it.
* Return 'def' is we can't parse any integer from the string.
*/
public static int parseInt(String s, int def)
Expand All @@ -433,7 +433,7 @@ public static int parseInt(String s, int def)
}

/**
* Try to parse a long integer from the specified String and return it.
* Try to parse a long integer from the specified String and returns it.
* Return 'def' is we can't parse any integer from the string.
*/
public static long parseLong(String s, long def)
Expand All @@ -449,7 +449,7 @@ public static long parseLong(String s, long def)
}

/**
* Try to parse a float from the specified String and return it.
* Try to parse a float from the specified String and returns it.
* Return 'def' is we can't parse any float from the string.
*/
public static float parseFloat(String s, float def)
Expand All @@ -465,7 +465,7 @@ public static float parseFloat(String s, float def)
}

/**
* Try to parse a double from the specified String and return it.
* Try to parse a double from the specified String and returns it.
* Return 'def' is we can't parse any double from the string.
*/
public static double parseDouble(String s, double def)
Expand All @@ -481,7 +481,7 @@ public static double parseDouble(String s, double def)
}

/**
* Try to parse a array of byte from the specified String and return it.
* Try to parse a array of byte from the specified String and returns it.
* Return 'def' is we can't parse any array of byte from the string.
*/
public static byte[] parseBytes(String s, byte[] def)
Expand All @@ -491,7 +491,80 @@ public static byte[] parseBytes(String s, byte[] def)

return s.getBytes();
}

/**
* Try to parse an array of int from the specified String and returns it.
* The array should be in the following format: [1,2,3,5]
* Return 'def' is we can't parse any int array from the string.
*/
public static int[] parseIntArray(String s, int[] def)
{
if (s == null)
return def;

try
{
if (s.charAt(0) == '[')
{
String[] arrayStr = s.substring(1, s.length() - 1).split(",");
int[] result = new int[arrayStr.length];

for(int i = 0; i < arrayStr.length; i++)
result[i] = Integer.parseInt(arrayStr[i]);

return result;
}
// assume single value as single entry array
else return new int[] { Integer.parseInt(s) };
}
catch (NumberFormatException E)
{
return def;
}
}

/**
* Try to parse a 2D array of int from the specified String and returns it.
* The array should be in the following format: [[1,2,3],[2,3,5,6],[7,6,5,4]]
* Return 'def' is we can't parse any 2D int array from the string.
*/
public static int[][] parseIntArray2D(String s, int[][] def)
{
if (s == null)
return def;

try
{
if (s.charAt(0) == '[')
{
// remove surrounding '[]'
String ss = s.substring(1, s.length() - 1);
// remove all opening sub array '['
ss = ss.replaceAll("\\[", "");

// get all sub array
String[] arrayStr = ss.split("\\]");
int[][] result = new int[arrayStr.length][];

for(int i = 0; i < arrayStr.length; i++)
{
// remove leading ',' if present
if (arrayStr[i].charAt(0) == ',') arrayStr[i] = arrayStr[i].substring(1);
// get sub array
result[i] = parseIntArray("[" + arrayStr[i] + "]", (def.length > i) ? def[i] : new int[] {0});
}

return result;
}
// assume single value as single entry 2D array
else return new int[][] {{ Integer.parseInt(s) }};
}
catch (NumberFormatException E)
{
return def;
}
}

/**
* Returns a <tt>String</tt> object representing the specified
* boolean. If the specified boolean is <code>true</code>, then
Expand Down

0 comments on commit 00ea079

Please sign in to comment.