Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.scriptbasic.classification.Utility;
import com.scriptbasic.executors.rightvalues.BasicDoubleValue;
import com.scriptbasic.executors.rightvalues.BasicLongValue;
import com.scriptbasic.executors.rightvalues.BasicStringValue;
import com.scriptbasic.interfaces.BasicRuntimeException;
import com.scriptbasic.log.Logger;
import com.scriptbasic.log.LoggerFactory;
Expand Down Expand Up @@ -161,4 +162,49 @@ static public Long clng(final Object arg) throws BasicRuntimeException {
}
return BasicLongValue.asLong(RightValueUtility.createRightValue(arg));
}

/**
* Returns a String containing the character associated with the specified
* character code.
*
* @param charcode
* argument is a Long that identifies a character
* @return character
* @throws BasicRuntimeException
* fail if incorrect character code
*/
@BasicFunction(classification = Utility.class)
static public String chr(final Object charcode) throws BasicRuntimeException {
if (charcode == null) {
throw new BasicRuntimeException("undef cannot be converted to character");
}
final Long code = BasicLongValue.asLong(RightValueUtility.createRightValue(charcode));
try {
return Character.toString(code.intValue());
} catch (IllegalArgumentException e) {
throw new BasicRuntimeException("Invalid character code: " + code);
}
}

/**
* Returns an Integer representing the character code corresponding
* to the first letter in a string.
*
* @param arg
* string argument
* @return character code corresponding to the first letter
* @throws BasicRuntimeException
* fail on empty input
*/
@BasicFunction(classification = Utility.class)
static public Integer asc(final Object arg) throws BasicRuntimeException {
if (arg == null) {
throw new BasicRuntimeException("undef cannot be converted to code");
}
final String str = BasicStringValue.asString(RightValueUtility.createRightValue(arg));
if (str == null || str.length() == 0) {
throw new BasicRuntimeException("empty string cannot be converted to code");
}
return Integer.valueOf(str.charAt(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ assert("clng_1", clng(2)=2)
assert("clng_2", clng("2")=2)
assert("clng_3", clng(true)=1)

' test function chr
assert("chr_1", chr(65)="A")
assert("chr_2", chr("65")="A")

' test function asc
assert("asc_1", asc("A")=65)
assert("asc_2", asc(2)=50)
assert("asc_3", asc("ABCD")=65)

print "DONE"