ParseUtil { public static long[] parseStringToLongArray(String s, int[] indices, int length, char delimiter) { long[] parsed = new long[indices.length]; int charIndex = s.length(); int parsedIndex = indices.length - 1; int stringIndex = length - 1; int power = 0; int c; boolean delimCurrent = false; boolean numeric = true; boolean numberFound = false; boolean dashSeen = false; while (--charIndex > 0 && parsedIndex >= 0) { c = s.charAt(charIndex); if (c == delimiter) { if (!numberFound && numeric) { numberFound = true; } if (!delimCurrent) { if (numberFound && indices[parsedIndex] == stringIndex--) { parsedIndex--; } delimCurrent = true; power = 0; dashSeen = false; numeric = true; } } else if (indices[parsedIndex] != stringIndex || c == '+' || !numeric) { delimCurrent = false; } else if (c >= '0' && c <= '9' && !dashSeen) { if (power > 18 || power == 17 && c == '9' && parsed[parsedIndex] > 223_372_036_854_775_807L) { parsed[parsedIndex] = Long.MAX_VALUE; } else { parsed[parsedIndex] += (c - '0') * ParseUtil.POWERS_OF_TEN[power++]; } delimCurrent = false; } else if (c == '-') { parsed[parsedIndex] *= -1L; delimCurrent = false; dashSeen = true; } else { if (numberFound) { LOG.error("Illegal character parsing string '{}' to long array: {}", s, s.charAt(charIndex)); return new long[indices.length]; } parsed[parsedIndex] = 0; numeric = false; } } if (parsedIndex > 0) { LOG.error("Not enough fields in string '{}' parsing to long array: {}", s, indices.length - parsedIndex); return new long[indices.length]; } return parsed; } private  ParseUtil(); static long parseHertz(String hertz); static int parseLastInt(String s, int i); static long parseLastLong(String s, long li); static double parseLastDouble(String s, double d); static String parseLastString(String s); static String byteArrayToHexString(byte[] bytes); static byte[] hexStringToByteArray(String digits); static byte[] asciiStringToByteArray(String text, int length); static byte[] longToByteArray(long value, int valueSize, int length); static long strToLong(String str, int size); static long byteArrayToLong(byte[] bytes, int size); static float byteArrayToFloat(byte[] bytes, int size, int fpBits); static long unsignedIntToLong(int unsignedValue); static long unsignedLongToSignedLong(long unsignedValue); static String hexStringToString(String hexString); static int parseIntOrDefault(String s, int defaultInt); static long parseLongOrDefault(String s, long defaultLong); static long parseUnsignedLongOrDefault(String s, long defaultLong); static double parseDoubleOrDefault(String s, double defaultDouble); static long parseDHMSOrDefault(String s, long defaultLong); static String parseUuidOrDefault(String s, String defaultStr); static String getSingleQuoteStringValue(String line); static String getDoubleQuoteStringValue(String line); static String getStringBetween(String line, char c); static int getFirstIntValue(String line); static int getNthIntValue(String line, int n); static String removeMatchingString(final String original, final String toRemove); static long[] parseStringToLongArray(String s, int[] indices, int length, char delimiter); static int countStringToLongArray(String s, char delimiter); static String getTextBetweenStrings(String text, String before, String after); static long filetimeToUtcMs(long filetime, boolean local); static String parseMmDdYyyyToYyyyMmDD(String dateString); static OffsetDateTime parseCimDateTimeToOffset(String cimDateTime); static boolean filePathStartsWith(List<String> prefixList, String path); static long parseDecimalMemorySizeToBinary(String size); static Pair<String, String> parsePnPDeviceIdToVendorProductId(String pnpDeviceId); static long parseLshwResourceString(String resources); static Pair<String, String> parseLspciMachineReadable(String line); static long parseLspciMemorySize(String line); static List<Integer> parseHyphenatedIntList(String str); static String parseUtAddrV6toIP(int[] utAddrV6); static long hexStringToLong(String hexString, long defaultValue); static String removeLeadingDots(String dotPrefixedStr); static final Pattern whitespacesColonWhitespace; static final Pattern whitespaces; static final Pattern notDigits; static final Pattern startWithNotDigits; static final Pattern slash; }
@Test public void testParseStringToLongArray() { int[] indices = { 1, 3 }; long now = System.currentTimeMillis(); String foo = String.format("The numbers are %d %d %d %d", 123, 456, 789, now); int count = ParseUtil.countStringToLongArray(foo, ' '); assertEquals(4, count); long[] result = ParseUtil.parseStringToLongArray(foo, indices, 4, ' '); assertEquals(456L, result[0]); assertEquals(now, result[1]); foo = String.format("The numbers are %d %d %d %d %s", 123, 456, 789, now, "709af748-5f8e-41b3-b73a-b440ef4406c8"); count = ParseUtil.countStringToLongArray(foo, ' '); assertEquals(4, count); result = ParseUtil.parseStringToLongArray(foo, indices, 4, ' '); assertEquals(456L, result[0]); assertEquals(now, result[1]); foo = String.format("The numbers are %d -%d %d +%d", 123, 456, 789, now); count = ParseUtil.countStringToLongArray(foo, ' '); assertEquals(4, count); result = ParseUtil.parseStringToLongArray(foo, indices, 4, ' '); assertEquals(-456L, result[0]); assertEquals(now, result[1]); foo = String.format("Invalid character %d %s %d %d", 123, "4v6", 789, now); count = ParseUtil.countStringToLongArray(foo, ' '); assertEquals(2, count); result = ParseUtil.parseStringToLongArray(foo, indices, 4, ' '); assertEquals(0, result[1]); foo = String.format("Exceeds max long %d %d %d 1%d", 123, 456, 789, Long.MAX_VALUE); result = ParseUtil.parseStringToLongArray(foo, indices, 4, ' '); assertEquals(Long.MAX_VALUE, result[1]); foo = String.format("String too short %d %d %d %d", 123, 456, 789, now); result = ParseUtil.parseStringToLongArray(foo, indices, 9, ' '); assertEquals(0, result[1]); foo = String.format("Array too short %d %d %d %d", 123, 456, 789, now); result = ParseUtil.parseStringToLongArray(foo, indices, 2, ' '); assertEquals(0, result[1]); foo = String.format("%d %d %d %d", 123, 456, 789, now); count = ParseUtil.countStringToLongArray(foo, ' '); assertEquals(4, count); foo = String.format("%d %d %d %d nonNumeric", 123, 456, 789, now); count = ParseUtil.countStringToLongArray(foo, ' '); assertEquals(4, count); foo = String.format("%d %d %d %d 123-456", 123, 456, 789, now); count = ParseUtil.countStringToLongArray(foo, ' '); assertEquals(4, count); }