|
6 | 6 | import com.scriptbasic.classification.Utility; |
7 | 7 | import com.scriptbasic.executors.rightvalues.BasicDoubleValue; |
8 | 8 | import com.scriptbasic.executors.rightvalues.BasicLongValue; |
| 9 | +import com.scriptbasic.executors.rightvalues.BasicStringValue; |
9 | 10 | import com.scriptbasic.interfaces.BasicRuntimeException; |
10 | 11 | import com.scriptbasic.log.Logger; |
11 | 12 | import com.scriptbasic.log.LoggerFactory; |
@@ -161,4 +162,47 @@ static public Long clng(final Object arg) throws BasicRuntimeException { |
161 | 162 | } |
162 | 163 | return BasicLongValue.asLong(RightValueUtility.createRightValue(arg)); |
163 | 164 | } |
| 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 | + } |
164 | 208 | } |
0 commit comments