1 /*
2 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
47 if(text==null) return null; // be defensive
48
49 int len = text.length();
50
51 // most of the texts are already in the collapsed form.
52 // so look for the first whitespace in the hope that we will
53 // never see it.
54 int s=0;
55 while(s<len) {
56 if(isWhiteSpace(text.charAt(s)))
57 break;
58 s++;
59 }
60 if(s==len)
61 // the input happens to be already collapsed.
62 return text;
63
64 // we now know that the input contains spaces.
65 // let's sit down and do the collapsing normally.
66
67 StringBuffer result = new StringBuffer(len /*allocate enough size to avoid re-allocation*/ );
68
69 if(s!=0) {
70 for( int i=0; i<s; i++ )
71 result.append(text.charAt(i));
72 result.append(' ');
73 }
74
75 boolean inStripMode = true;
76 for (int i = s+1; i < len; i++) {
77 char ch = text.charAt(i);
78 boolean b = isWhiteSpace(ch);
79 if (inStripMode && b)
80 continue; // skip this character
81
82 inStripMode = b;
83 if (inStripMode)
84 result.append(' ');
85 else
86 result.append(ch);
87 }
|
1 /*
2 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
47 if(text==null) return null; // be defensive
48
49 int len = text.length();
50
51 // most of the texts are already in the collapsed form.
52 // so look for the first whitespace in the hope that we will
53 // never see it.
54 int s=0;
55 while(s<len) {
56 if(isWhiteSpace(text.charAt(s)))
57 break;
58 s++;
59 }
60 if(s==len)
61 // the input happens to be already collapsed.
62 return text;
63
64 // we now know that the input contains spaces.
65 // let's sit down and do the collapsing normally.
66
67 StringBuilder result = new StringBuilder(len /*allocate enough size to avoid re-allocation*/ );
68
69 if(s!=0) {
70 for( int i=0; i<s; i++ )
71 result.append(text.charAt(i));
72 result.append(' ');
73 }
74
75 boolean inStripMode = true;
76 for (int i = s+1; i < len; i++) {
77 char ch = text.charAt(i);
78 boolean b = isWhiteSpace(ch);
79 if (inStripMode && b)
80 continue; // skip this character
81
82 inStripMode = b;
83 if (inStripMode)
84 result.append(' ');
85 else
86 result.append(ch);
87 }
|