public static String collapse(String v,
int wsr) {
if (wsr == SchemaType.WS_PRESERVE || wsr == SchemaType.WS_UNSPECIFIED)
return v;
if (v.indexOf('\n') >= 0)
v = v.replace('\n', ' ');
if (v.indexOf('\t') >= 0)
v = v.replace('\t', ' ');
if (v.indexOf('\r') >= 0)
v = v.replace('\r', ' ');
if (wsr == SchemaType.WS_REPLACE)
return v;
int j = 0;
int len = v.length();
if (len == 0)
return v;
int i;
/* a trick: examine every other character looking for pairs of spaces */
if (v.charAt(0) != ' ')
{
examine: {
for (j = 2; j < len; j += 2)
{
if (v.charAt(j) == ' ')
{
if (v.charAt(j - 1) == ' ')
break examine;
if (j == len - 1)
break examine;
j++;
if (v.charAt(j) == ' ')
break examine;
}
}
if (j == len && v.charAt(j - 1) == ' ')
break examine;
return v;
}
/* j is pointing at the first ws to be removed, or past end */
i = j;
}
else
{
/**
* j is pointing at the last whitespace in the initial run
*/
while (j + 1 < v.length() && v.charAt(j + 1) == ' ')
j += 1;
i = 0;
}
char[] ch = v.toCharArray();
shifter: for (;;)
{
for (;;)
{
/* j was ws or past end */
j++;
if (j >= len)
break shifter;
if (v.charAt(j) != ' ')
break;
}
for (;;)
{
/* j was nonws */
ch[i++] = ch[j++];
if (j >= len)
break shifter;
if (ch[j] == ' ')
{
ch[i++] = ch[j++];
if (j >= len)
break shifter;
if (ch[j] == ' ')
break;
}
}
}
return new String(ch, 0, (i == 0 || ch[i - 1] != ' ') ? i : i - 1);
}
The algorithm used by apply_wscanon: sometimes used in impls. |