Skip to content

Commit f15f414

Browse files
committed
String functions (trim, left, right, mid) - supports null parameter and numerical parameter, improved VB compatibility
1 parent 58278a9 commit f15f414

2 files changed

Lines changed: 49 additions & 12 deletions

File tree

src/main/java/com/scriptbasic/utility/functions/StringFunctions.java

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@ static public String chomp(final String s) {
5757
*/
5858
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
5959
com.scriptbasic.classification.Utility.class})
60-
static public String ltrim(final String s) {
61-
return s.replaceAll("^\\s*", "");
60+
static public String ltrim(final Object s) {
61+
if(s==null) {
62+
return null;
63+
}
64+
return s.toString().replaceAll("^\\s*", "");
6265
}
6366

6467
/**
@@ -69,54 +72,69 @@ static public String ltrim(final String s) {
6972
*/
7073
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
7174
com.scriptbasic.classification.Utility.class})
72-
static public String rtrim(final String s) {
73-
return s.replaceAll("\\s*$", "");
75+
static public String rtrim(final Object s) {
76+
if(s==null) {
77+
return null;
78+
}
79+
return s.toString().replaceAll("\\s*$", "");
7480
}
7581

7682
/**
7783
* Return {@code len} number of characters from the left (the beginning) of the
7884
* string.
7985
*
80-
* @param s parameter
86+
* @param o parameter
8187
* @param len parameter
8288
* @return return value
8389
*/
8490
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
8591
com.scriptbasic.classification.Utility.class})
86-
static public String left(final String s, final int len) {
92+
static public String left(final Object o, final int len) {
93+
if(o==null) {
94+
return null;
95+
}
96+
String s = o.toString();
8797
return s.length() > len ? s.substring(0, len) : s;
8898
}
8999

90100
/**
91101
* Return a substring from the string that starts at the position
92102
* {@code start} and has a length of {@code len}.
93103
*
94-
* @param s parameter
104+
* @param o parameter
95105
* @param start parameter
96106
* @param len parameter
97107
* @return return value
98108
* @throws BasicRuntimeException incorrect parameter
99109
*/
100110
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
101111
com.scriptbasic.classification.Utility.class})
102-
static public String mid(final String s, final int start, final int len) throws BasicRuntimeException {
112+
static public String mid(final Object o, final int start, final int len) throws BasicRuntimeException {
103113
if (start < 1) {
104114
throw new BasicRuntimeException("Incorrect value in parameter start: " + start);
105115
}
116+
if(o==null) {
117+
return null;
118+
}
119+
String s = o.toString();
106120
return s.substring(start - 1, start - 1 + len);
107121
}
108122

109123
/**
110124
* Return {@code len} number of characters from the right (the end) of the
111125
* string.
112126
*
113-
* @param s parameter
127+
* @param o parameter
114128
* @param len parameter
115129
* @return return value
116130
*/
117131
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
118132
com.scriptbasic.classification.Utility.class})
119-
static public String right(final String s, final int len) {
133+
static public String right(final Object o, final int len) {
134+
if(o==null) {
135+
return null;
136+
}
137+
String s = o.toString();
120138
return s.length() > len ? s.substring(s.length() - len) : s;
121139
}
122140

@@ -185,8 +203,11 @@ static public String lcase(final String s) {
185203
}
186204

187205
@BasicFunction(classification = {com.scriptbasic.classification.String.class})
188-
static public String trim(final String s) {
189-
return s.trim();
206+
static public String trim(final Object s) {
207+
if(s==null) {
208+
return null;
209+
}
210+
return s.toString().trim();
190211
}
191212

192213
/**

src/test/resources/com/scriptbasic/testprograms/TestStringFunctions.bas

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
' Test ltrim, trim, rtrim functions
2+
v = 1234
3+
assert("ltrim_with_num", ltrim(v)="1234")
4+
assert("trim_with_num", trim(v)="1234")
5+
assert("rtrim_with_num", rtrim(v)="1234")
6+
7+
v = " 1234 "
8+
assert("ltrim", ltrim(v)="1234 ")
9+
assert("trim", trim(v)="1234")
10+
assert("rtrim", rtrim(v)=" 1234")
11+
12+
' Test left, mid, right functions
13+
v = 1234
14+
assert("left_with_num", left(v,2)="12")
15+
assert("mid_with_num", mid(v,2,3)="234")
16+
assert("right_with_num", right(v,2)="34")
117
v = "0123456789"
218
PRINT left(v,2)
319
PRINT RIGHT(v,2)

0 commit comments

Comments
 (0)