Skip to content

Commit 7a9facf

Browse files
authored
Merge pull request #22 from PetrPytelka/string_functions_with_non_string_param
String functions (trim, left, right, mid) - supports non string parameter
2 parents 58278a9 + 1666063 commit 7a9facf

File tree

3 files changed

+89
-11
lines changed

3 files changed

+89
-11
lines changed

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

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.scriptbasic.utility.functions;
22

33
import com.scriptbasic.api.BasicFunction;
4+
import com.scriptbasic.executors.rightvalues.BasicStringValue;
45
import com.scriptbasic.interfaces.BasicRuntimeException;
6+
import com.scriptbasic.utility.RightValueUtility;
57

68
/**
79
* <p>
@@ -49,74 +51,116 @@ static public String chomp(final String s) {
4951
return s.replaceAll("\\n*$", "");
5052
}
5153

54+
/**
55+
* Returns string for the argument.
56+
* @param o argument to be converted
57+
* @return string value
58+
* @throws BasicRuntimeException failed to convert to string
59+
*/
60+
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
61+
com.scriptbasic.classification.Utility.class})
62+
static public String cstr(final Object o) throws BasicRuntimeException {
63+
if (o == null) {
64+
throw new BasicRuntimeException("Null cannot be converted to string");
65+
}
66+
if (o instanceof String) {
67+
return (String) o;
68+
}
69+
return BasicStringValue.asString(RightValueUtility.createRightValue(o));
70+
}
71+
5272
/**
5373
* Trim the white spaces from the start of the string.
5474
*
55-
* @param s the string to trim.
75+
* @param o the string to trim.
5676
* @return the trimmed string
77+
* @throws BasicRuntimeException if cannot convert to string
5778
*/
5879
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
5980
com.scriptbasic.classification.Utility.class})
60-
static public String ltrim(final String s) {
81+
static public String ltrim(final Object o) throws BasicRuntimeException {
82+
if (o == null) {
83+
return null;
84+
}
85+
final String s = cstr(o);
6186
return s.replaceAll("^\\s*", "");
6287
}
6388

6489
/**
6590
* Trim the white spaces from the end of the string.
6691
*
67-
* @param s the string to trim
92+
* @param o the string to trim
6893
* @return the trimmed string
94+
* @throws BasicRuntimeException if cannot convert to string
6995
*/
7096
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
7197
com.scriptbasic.classification.Utility.class})
72-
static public String rtrim(final String s) {
98+
static public String rtrim(final Object o) throws BasicRuntimeException {
99+
if (o == null) {
100+
return null;
101+
}
102+
final String s = cstr(o);
73103
return s.replaceAll("\\s*$", "");
74104
}
75105

76106
/**
77107
* Return {@code len} number of characters from the left (the beginning) of the
78108
* string.
79109
*
80-
* @param s parameter
110+
* @param o parameter
81111
* @param len parameter
82112
* @return return value
113+
* @throws BasicRuntimeException if cannot convert to string
83114
*/
84115
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
85116
com.scriptbasic.classification.Utility.class})
86-
static public String left(final String s, final int len) {
117+
static public String left(final Object o, final int len) throws BasicRuntimeException {
118+
if (o == null) {
119+
return null;
120+
}
121+
final String s = cstr(o);
87122
return s.length() > len ? s.substring(0, len) : s;
88123
}
89124

90125
/**
91126
* Return a substring from the string that starts at the position
92127
* {@code start} and has a length of {@code len}.
93128
*
94-
* @param s parameter
129+
* @param o parameter
95130
* @param start parameter
96131
* @param len parameter
97132
* @return return value
98133
* @throws BasicRuntimeException incorrect parameter
99134
*/
100135
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
101136
com.scriptbasic.classification.Utility.class})
102-
static public String mid(final String s, final int start, final int len) throws BasicRuntimeException {
137+
static public String mid(final Object o, final int start, final int len) throws BasicRuntimeException {
103138
if (start < 1) {
104139
throw new BasicRuntimeException("Incorrect value in parameter start: " + start);
105140
}
141+
if (o == null) {
142+
return null;
143+
}
144+
final String s = cstr(o);
106145
return s.substring(start - 1, start - 1 + len);
107146
}
108147

109148
/**
110149
* Return {@code len} number of characters from the right (the end) of the
111150
* string.
112151
*
113-
* @param s parameter
152+
* @param o parameter
114153
* @param len parameter
115154
* @return return value
155+
* @throws BasicRuntimeException if cannot convert to string
116156
*/
117157
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
118158
com.scriptbasic.classification.Utility.class})
119-
static public String right(final String s, final int len) {
159+
static public String right(final Object o, final int len) throws BasicRuntimeException {
160+
if (o == null) {
161+
return null;
162+
}
163+
final String s = cstr(o);
120164
return s.length() > len ? s.substring(s.length() - len) : s;
121165
}
122166

@@ -185,7 +229,11 @@ static public String lcase(final String s) {
185229
}
186230

187231
@BasicFunction(classification = {com.scriptbasic.classification.String.class})
188-
static public String trim(final String s) {
232+
static public String trim(final Object o) throws BasicRuntimeException {
233+
if (o == null) {
234+
return null;
235+
}
236+
final String s = cstr(o);
189237
return s.trim();
190238
}
191239

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ static public Boolean isDefined(final Object s) {
8585
return s != null;
8686
}
8787

88+
/**
89+
* @param s is some value or object
90+
* @return true if the parameter is null
91+
*/
92+
@BasicFunction(classification = Utility.class)
93+
static public Boolean isNull(final Object s) {
94+
return s == null;
95+
}
96+
8897
/**
8998
* Create a new byte buffer of length {@code len}.
9099
*

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
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 null values
13+
assert("ltrim_with_null", IsNull(ltrim(undefined_var)))
14+
assert("trim_with_null", IsNull(trim(undefined_var)))
15+
assert("rtrim_with_null", IsNull(rtrim(undefined_var)))
16+
17+
' Test left, mid, right functions
18+
v = 1234
19+
assert("left_with_num", left(v,2)="12")
20+
assert("mid_with_num", mid(v,2,3)="234")
21+
assert("right_with_num", right(v,2)="34")
122
v = "0123456789"
223
PRINT left(v,2)
324
PRINT RIGHT(v,2)

0 commit comments

Comments
 (0)