Crossfire JXClient, Trunk  R20561
StringUtils.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.util;
23 
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.regex.Pattern;
27 import org.jetbrains.annotations.NotNull;
28 
33 public class StringUtils {
34 
38  @NotNull
39  private static final Pattern PATTERN_LEADING_WHITESPACE = Pattern.compile("^[ \t]+");
40 
44  private StringUtils() {
45  }
46 
52  @NotNull
53  public static String trimLeading(@NotNull final CharSequence str) {
54  return PATTERN_LEADING_WHITESPACE.matcher(str).replaceAll("");
55  }
56 
63  @NotNull
64  public static String[] splitFields(@NotNull final String line) throws UnterminatedTokenException {
65  final List<String> tokens = new ArrayList<>(64);
66 
67  final char[] chars = line.toCharArray();
68 
69  int i = 0;
70  while (i < chars.length) {
71  while (i < chars.length && (chars[i] == ' ' || chars[i] == '\t')) {
72  i++;
73  }
74  final int start;
75  final int end;
76  if (i < chars.length && (chars[i] == '"' || chars[i] == '\'')) {
77  // quoted token
78  final char quoteChar = chars[i++];
79  start = i;
80  while (i < chars.length && chars[i] != quoteChar) {
81  i++;
82  }
83  if (i >= chars.length) {
84  throw new UnterminatedTokenException(line.substring(start-1));
85  }
86  end = i;
87  i++;
88  } else {
89  // unquoted token
90  start = i;
91  while (i < chars.length && chars[i] != ' ' && chars[i] != '\t') {
92  i++;
93  }
94  end = i;
95  }
96  tokens.add(line.substring(start, end));
97  }
98 
99  return tokens.toArray(new String[tokens.size()]);
100  }
101 
102 }
StringUtils()
Private constructor to prevent instantiation.
Utility class for string manipulation.
static final Pattern PATTERN_LEADING_WHITESPACE
A pattern matching leading whitespace.
static String [] splitFields(@NotNull final String line)
Splits a line into tokens.
static String trimLeading(@NotNull final CharSequence str)
Removes leading whitespace from a string.