Crossfire JXClient, Trunk
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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.util;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.regex.Pattern;
28 import org.jetbrains.annotations.NotNull;
29 
34 public class StringUtils {
35 
39  @NotNull
40  private static final Pattern PATTERN_LEADING_WHITESPACE = Pattern.compile("^[ \t]+");
41 
45  private StringUtils() {
46  }
47 
53  @NotNull
54  public static String trimLeading(@NotNull final CharSequence str) {
55  return PATTERN_LEADING_WHITESPACE.matcher(str).replaceAll("");
56  }
57 
64  @NotNull
65  public static String @NotNull [] splitFields(@NotNull final String line) throws UnterminatedTokenException {
66  final List<String> tokens = new ArrayList<>(64);
67 
68  final char[] chars = line.toCharArray();
69 
70  int i = 0;
71  while (i < chars.length) {
72  while (i < chars.length && (chars[i] == ' ' || chars[i] == '\t')) {
73  i++;
74  }
75  final int start;
76  final int end;
77  if (i < chars.length && (chars[i] == '"' || chars[i] == '\'')) {
78  // quoted token
79  final char quoteChar = chars[i++];
80  start = i;
81  while (i < chars.length && chars[i] != quoteChar) {
82  i++;
83  }
84  if (i >= chars.length) {
85  throw new UnterminatedTokenException(line.substring(start-1));
86  }
87  end = i;
88  i++;
89  } else {
90  // unquoted token
91  start = i;
92  while (i < chars.length && chars[i] != ' ' && chars[i] != '\t') {
93  i++;
94  }
95  end = i;
96  }
97  tokens.add(line.substring(start, end));
98  }
99 
100  return tokens.toArray(new String[tokens.size()]);
101  }
102 
103 }
com.realtime.crossfire.jxclient.util.StringUtils.trimLeading
static String trimLeading(@NotNull final CharSequence str)
Definition: StringUtils.java:54
com.realtime.crossfire.jxclient.util.UnterminatedTokenException
Definition: UnterminatedTokenException.java:31
com.realtime.crossfire.jxclient.util.StringUtils.splitFields
static String[] splitFields(@NotNull final String line)
Definition: StringUtils.java:65
com.realtime.crossfire.jxclient.util.StringUtils.PATTERN_LEADING_WHITESPACE
static final Pattern PATTERN_LEADING_WHITESPACE
Definition: StringUtils.java:40
com.realtime.crossfire.jxclient.util.StringUtils
Definition: StringUtils.java:34
com.realtime.crossfire.jxclient.util.StringUtils.StringUtils
StringUtils()
Definition: StringUtils.java:45