Skip to content

Commit 0f2e3e8

Browse files
committed
New functions CHR and ASC
1 parent 7dc3784 commit 0f2e3e8

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.scriptbasic.classification.Utility;
77
import com.scriptbasic.executors.rightvalues.BasicDoubleValue;
88
import com.scriptbasic.executors.rightvalues.BasicLongValue;
9+
import com.scriptbasic.executors.rightvalues.BasicStringValue;
910
import com.scriptbasic.interfaces.BasicRuntimeException;
1011
import com.scriptbasic.log.Logger;
1112
import com.scriptbasic.log.LoggerFactory;
@@ -161,4 +162,47 @@ static public Long clng(final Object arg) throws BasicRuntimeException {
161162
}
162163
return BasicLongValue.asLong(RightValueUtility.createRightValue(arg));
163164
}
165+
166+
/**
167+
* Returns a String containing the character associated with the specified
168+
* character code.
169+
*
170+
* @param charcode
171+
* argument is a Long that identifies a character
172+
* @return character
173+
* @throws BasicRuntimeException
174+
* fail if incorrect character code
175+
*/
176+
@BasicFunction(classification = Utility.class)
177+
static public String chr(final Object charcode) throws BasicRuntimeException {
178+
if (charcode == null) {
179+
throw new BasicRuntimeException("undef cannot be converted character");
180+
}
181+
final Long code = BasicLongValue.asLong(RightValueUtility.createRightValue(charcode));
182+
try {
183+
return Character.toString(code.intValue());
184+
} catch (IllegalArgumentException e) {
185+
throw new BasicRuntimeException("Invalid character code: " + code);
186+
}
187+
}
188+
189+
/**
190+
* Returns an Integer representing the character code corresponding
191+
* to the first letter in a string.
192+
*
193+
* @param arg
194+
* string argument
195+
* @return character code corresponding to the first letter
196+
*/
197+
@BasicFunction(classification = Utility.class)
198+
static public Integer asc(final Object arg) throws BasicRuntimeException {
199+
if (arg == null) {
200+
throw new BasicRuntimeException("undef cannot be converted to code");
201+
}
202+
final String str = BasicStringValue.asString(RightValueUtility.createRightValue(arg));
203+
if (str == null || str.length() == 0) {
204+
throw new BasicRuntimeException("empty string cannot be converted to code");
205+
}
206+
return Integer.valueOf(str.charAt(0));
207+
}
164208
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,13 @@ assert("clng_1", clng(2)=2)
99
assert("clng_2", clng("2")=2)
1010
assert("clng_3", clng(true)=1)
1111

12+
' test function chr
13+
assert("chr_1", chr(65)="A")
14+
assert("chr_2", chr("65")="A")
15+
16+
' test function asc
17+
assert("asc_1", asc("A")=65)
18+
assert("asc_2", asc(2)=50)
19+
assert("asc_3", asc("ABCD")=65)
20+
1221
print "DONE"

0 commit comments

Comments
 (0)