diff options
| author | bananeweizen <bananeweizen@gmx.de> | 2011-10-01 09:23:30 +0200 |
|---|---|---|
| committer | bananeweizen <bananeweizen@gmx.de> | 2011-10-01 09:23:30 +0200 |
| commit | c38724da4d3b9ea03a522e503d7988b0a7ce63e4 (patch) | |
| tree | a118c51d9941a85a8401a92122172c035961a16f /main/src/cgeo/geocaching/utils/BaseUtils.java | |
| parent | 0f3e9175b18394e08fc09c66e7e00b84e5c95f1c (diff) | |
| parent | 9393d3815bc9211091b3eef5f1c79c4c976fdc7b (diff) | |
| download | cgeo-c38724da4d3b9ea03a522e503d7988b0a7ce63e4.zip cgeo-c38724da4d3b9ea03a522e503d7988b0a7ce63e4.tar.gz cgeo-c38724da4d3b9ea03a522e503d7988b0a7ce63e4.tar.bz2 | |
Merge #510
Conflicts:
main/src/cgeo/geocaching/cgBase.java
tests/src/cgeo/geocaching/test/mock/MockedCache.java
Diffstat (limited to 'main/src/cgeo/geocaching/utils/BaseUtils.java')
| -rw-r--r-- | main/src/cgeo/geocaching/utils/BaseUtils.java | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/utils/BaseUtils.java b/main/src/cgeo/geocaching/utils/BaseUtils.java new file mode 100644 index 0000000..5f4833b --- /dev/null +++ b/main/src/cgeo/geocaching/utils/BaseUtils.java @@ -0,0 +1,67 @@ +/** + * + */ +package cgeo.geocaching.utils; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Misc. utils + */ +public final class BaseUtils { + + /** + * Searches for the pattern p in the data for the n-th group. If the pattern + * is not found defaultValue is returned + * + * @param data + * @param p + * @param group + * @param defaultValue + * @return + */ + public static String getMatch(final String data, final Pattern p, final int group, final String defaultValue) { + final Matcher matcher = p.matcher(data); + if (matcher.find() && matcher.groupCount() >= group) { + // creating a new String via String constructor is necessary here!! + return new String(matcher.group(group).trim()); + // Java copies the whole page String, when matching with regular expressions + // later this would block the garbage collector, as we only need tiny parts of the page + // see http://developer.android.com/reference/java/lang/String.html#backing_array + + // And BTW: You cannot even see that effect in the debugger, but must use a separate memory profiler! + } + return defaultValue; + } + + /** + * Replace the characters \n, \r and \t with a space + * The result is a very long single "line". + * Don't change this behavior - the patterns for parsing rely on this matter of fact ! + * + * @param buffer + * The data + */ + public static void replaceWhitespace(final StringBuffer buffer) { + final int length = buffer.length(); + final char[] chars = new char[length]; + buffer.getChars(0, length, chars, 0); + int resultSize = 0; + boolean lastWasWhitespace = false; + for (char c : chars) { + if (c == ' ' || c == '\n' || c == '\r' || c == '\t') { + if (!lastWasWhitespace) { + chars[resultSize++] = ' '; + } + lastWasWhitespace = true; + } else { + chars[resultSize++] = c; + lastWasWhitespace = false; + } + } + buffer.setLength(0); + buffer.append(chars); + } + +} |
