| 1 | /* |
| 2 | * Copyright (c) 2012-2016, Luigi R. Viggiano |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This software is distributable under the BSD license. |
| 6 | * See the terms of the BSD license in the documentation provided with this software. |
| 7 | */ |
| 8 | |
| 9 | package org.aeonbits.owner.converters; |
| 10 | |
| 11 | /** |
| 12 | * A utility class for converters. |
| 13 | * |
| 14 | * @author Stefan Freyr Stefansson |
| 15 | */ |
| 16 | class ConverterUtil { |
| 17 | |
| 18 | // Suppresses default constructor, ensuring no one instantiate this class. |
| 19 | private ConverterUtil() {} |
| 20 | |
| 21 | /** |
| 22 | * Splits a string into a numeric part and a character part. The input string should conform to the format |
| 23 | * <code>[numeric_part][char_part]</code> with an optional whitespace between the two parts. |
| 24 | * |
| 25 | * The <code>char_part</code> should only contain letters as defined by {@link Character#isLetter(char)} while |
| 26 | * the <code>numeric_part</code> will be parsed regardless of content. |
| 27 | * |
| 28 | * Any whitespace will be trimmed from the beginning and end of both parts, however, the <code>numeric_part</code> |
| 29 | * can contain whitespaces within it. |
| 30 | * |
| 31 | * @param input the string to split. |
| 32 | * |
| 33 | * @return an array of two strings. |
| 34 | */ |
| 35 | static String[] splitNumericAndChar(String input){ |
| 36 | // ATTN: String.trim() may not trim all UTF-8 whitespace characters properly. |
| 37 | // The original implementation used its own unicodeTrim() method that I decided not to include until the need |
| 38 | // arises. For more information, see: |
| 39 | // https://github.com/typesafehub/config/blob/v1.3.0/config/src/main/java/com/typesafe/config/impl/ConfigImplUtil.java#L118-L164 |
| 40 | |
| 41 | int i = input.length() - 1; |
| 42 | while (i >= 0) { |
| 43 | char c = input.charAt(i); |
| 44 | if (!Character.isLetter(c)) |
| 45 | break; |
| 46 | i -= 1; |
| 47 | } |
| 48 | return new String[]{input.substring(0,i+1).trim(), input.substring(i + 1).trim()}; |
| 49 | } |
| 50 | |
| 51 | } |