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
@@ -1,7 +1,9 @@
package com.scriptbasic.utility.functions;

import com.scriptbasic.api.BasicFunction;
import com.scriptbasic.executors.rightvalues.BasicStringValue;
import com.scriptbasic.interfaces.BasicRuntimeException;
import com.scriptbasic.utility.RightValueUtility;

/**
* <p>
Expand Down Expand Up @@ -49,74 +51,116 @@ static public String chomp(final String s) {
return s.replaceAll("\\n*$", "");
}

/**
* Returns string for the argument.
* @param o argument to be converted
* @return string value
* @throws BasicRuntimeException failed to convert to string
*/
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
com.scriptbasic.classification.Utility.class})
static public String cstr(final Object o) throws BasicRuntimeException {
if (o == null) {
throw new BasicRuntimeException("Null cannot be converted to string");
}
if (o instanceof String) {
return (String) o;
}
return BasicStringValue.asString(RightValueUtility.createRightValue(o));
}

/**
* Trim the white spaces from the start of the string.
*
* @param s the string to trim.
* @param o the string to trim.
* @return the trimmed string
* @throws BasicRuntimeException if cannot convert to string
*/
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
com.scriptbasic.classification.Utility.class})
static public String ltrim(final String s) {
static public String ltrim(final Object o) throws BasicRuntimeException {
if (o == null) {
return null;
}
final String s = cstr(o);
return s.replaceAll("^\\s*", "");
}

/**
* Trim the white spaces from the end of the string.
*
* @param s the string to trim
* @param o the string to trim
* @return the trimmed string
* @throws BasicRuntimeException if cannot convert to string
*/
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
com.scriptbasic.classification.Utility.class})
static public String rtrim(final String s) {
static public String rtrim(final Object o) throws BasicRuntimeException {
if (o == null) {
return null;
}
final String s = cstr(o);
return s.replaceAll("\\s*$", "");
}

/**
* Return {@code len} number of characters from the left (the beginning) of the
* string.
*
* @param s parameter
* @param o parameter
* @param len parameter
* @return return value
* @throws BasicRuntimeException if cannot convert to string
*/
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
com.scriptbasic.classification.Utility.class})
static public String left(final String s, final int len) {
static public String left(final Object o, final int len) throws BasicRuntimeException {
if (o == null) {
return null;
}
final String s = cstr(o);
return s.length() > len ? s.substring(0, len) : s;
}

/**
* Return a substring from the string that starts at the position
* {@code start} and has a length of {@code len}.
*
* @param s parameter
* @param o parameter
* @param start parameter
* @param len parameter
* @return return value
* @throws BasicRuntimeException incorrect parameter
*/
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
com.scriptbasic.classification.Utility.class})
static public String mid(final String s, final int start, final int len) throws BasicRuntimeException {
static public String mid(final Object o, final int start, final int len) throws BasicRuntimeException {
if (start < 1) {
throw new BasicRuntimeException("Incorrect value in parameter start: " + start);
}
if (o == null) {
return null;
}
final String s = cstr(o);
return s.substring(start - 1, start - 1 + len);
}

/**
* Return {@code len} number of characters from the right (the end) of the
* string.
*
* @param s parameter
* @param o parameter
* @param len parameter
* @return return value
* @throws BasicRuntimeException if cannot convert to string
*/
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
com.scriptbasic.classification.Utility.class})
static public String right(final String s, final int len) {
static public String right(final Object o, final int len) throws BasicRuntimeException {
if (o == null) {
return null;
}
final String s = cstr(o);
return s.length() > len ? s.substring(s.length() - len) : s;
}

Expand Down Expand Up @@ -185,7 +229,11 @@ static public String lcase(final String s) {
}

@BasicFunction(classification = {com.scriptbasic.classification.String.class})
static public String trim(final String s) {
static public String trim(final Object o) throws BasicRuntimeException {
if (o == null) {
return null;
}
final String s = cstr(o);
return s.trim();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ static public Boolean isDefined(final Object s) {
return s != null;
}

/**
* @param s is some value or object
* @return true if the parameter is null
*/
@BasicFunction(classification = Utility.class)
static public Boolean isNull(final Object s) {
return s == null;
}

/**
* Create a new byte buffer of length {@code len}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
' Test ltrim, trim, rtrim functions
v = 1234
assert("ltrim_with_num", ltrim(v)="1234")
assert("trim_with_num", trim(v)="1234")
assert("rtrim_with_num", rtrim(v)="1234")

v = " 1234 "
assert("ltrim", ltrim(v)="1234 ")
assert("trim", trim(v)="1234")
assert("rtrim", rtrim(v)=" 1234")

' Test null values
assert("ltrim_with_null", IsNull(ltrim(undefined_var)))
assert("trim_with_null", IsNull(trim(undefined_var)))
assert("rtrim_with_null", IsNull(rtrim(undefined_var)))

' Test left, mid, right functions
v = 1234
assert("left_with_num", left(v,2)="12")
assert("mid_with_num", mid(v,2,3)="234")
assert("right_with_num", right(v,2)="34")
v = "0123456789"
PRINT left(v,2)
PRINT RIGHT(v,2)
Expand Down