-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (c) 2018 Villu Ruusmann | ||
*/ | ||
package org.jpmml.model; | ||
|
||
public class StringUtil { | ||
|
||
private StringUtil(){ | ||
} | ||
|
||
/** | ||
* <p> | ||
* Trims trailing whitespace from a string value. | ||
* According to the PMML specification, | ||
* the leading whitespace is significant, but the trailing whitespace isn't. | ||
* </p> | ||
* | ||
* @see Character#isWhitespace(char) | ||
*/ | ||
static | ||
public String trim(String string){ | ||
int length = string.length(); | ||
|
||
int trimmedLength = length; | ||
|
||
while(trimmedLength > 0){ | ||
char c = string.charAt(trimmedLength - 1); | ||
|
||
if(!Character.isWhitespace(c)){ | ||
break; | ||
} | ||
|
||
trimmedLength--; | ||
} | ||
|
||
if(trimmedLength < length){ | ||
string = string.substring(0, trimmedLength); | ||
} | ||
|
||
return string; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
pmml-model/src/test/java/org/jpmml/model/StringUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) 2018 Villu Ruusmann | ||
*/ | ||
package org.jpmml.model; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertSame; | ||
|
||
public class StringUtilTest { | ||
|
||
@Test | ||
public void trim(){ | ||
String string = ""; | ||
|
||
assertSame(string, StringUtil.trim(string)); | ||
|
||
string = "token"; | ||
|
||
assertSame(string, StringUtil.trim(string)); | ||
|
||
string = "\ttoken"; | ||
|
||
assertSame(string, StringUtil.trim(string)); | ||
|
||
string = "token\n"; | ||
|
||
assertEquals("token", StringUtil.trim(string)); | ||
|
||
string = "token\r\n"; | ||
|
||
assertEquals("token", StringUtil.trim(string)); | ||
|
||
string = "\ttoken\n"; | ||
|
||
assertEquals("\ttoken", StringUtil.trim(string)); | ||
|
||
string = "\ttoken\r\n"; | ||
|
||
assertEquals("\ttoken", StringUtil.trim(string)); | ||
} | ||
} |