Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
StringUtils | 28 | 14 | 0% | 4 | 0 |
1 | /* | |
2 | * See the NOTICE file distributed with this work for additional | |
3 | * information regarding copyright ownership. | |
4 | * | |
5 | * This is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License as | |
7 | * published by the Free Software Foundation; either version 2.1 of | |
8 | * the License, or (at your option) any later version. | |
9 | * | |
10 | * This software is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this software; if not, write to the Free | |
17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
19 | */ | |
20 | package org.xwiki.text; | |
21 | ||
22 | /** | |
23 | * Extends {@link org.apache.commons.lang3.StringUtils} with some more useful tools. | |
24 | * | |
25 | * @version $Id: eb7bbcaf336c6f791fdce502b7443f350229e2ae $ | |
26 | * @since 6.2 | |
27 | */ | |
28 | public class StringUtils extends org.apache.commons.lang3.StringUtils | |
29 | { | |
30 | /** | |
31 | * An attempt to make doubling a character (usually for escaping purposes) as fast as it can be. A lot faster than | |
32 | * the usual <code>mystring.replace("a", "aa")</code> for example. | |
33 | * | |
34 | * @param str the string to modify | |
35 | * @param c the character to double | |
36 | * @return the modified string | |
37 | */ | |
38 | 7 | ![]() |
39 | { | |
40 | 7 | if (isEmpty(str)) { |
41 | 2 | return str; |
42 | } | |
43 | ||
44 | 5 | int start = 0; |
45 | 5 | int end = str.indexOf(c, start); |
46 | 5 | if (end == INDEX_NOT_FOUND) { |
47 | 1 | return str; |
48 | } | |
49 | ||
50 | 4 | final StringBuilder buf = new StringBuilder(str.length() + 1); |
51 | ||
52 | 4 | do { |
53 | 7 | end += 1; |
54 | 7 | buf.append(str.substring(start, end)).append(c); |
55 | ||
56 | 7 | start = end; |
57 | 7 | end = str.indexOf(c, start); |
58 | 7 | } while (end != INDEX_NOT_FOUND); |
59 | ||
60 | 4 | buf.append(str.substring(start)); |
61 | ||
62 | 4 | return buf.toString(); |
63 | } | |
64 | } |