Java "strlib"

Admin User, erstellt 11. Apr. 2024
         
package liblet.util;
import nova.envir.ComputeCompare;
import nova.envir.ComputeElem;
import nova.Machine;
import nova.Special;
import nova.Store;
import java.util.Locale;
public final class strlib {
private static boolean test_sys_float_atom(Object[] args) {
Object value = Machine.deref(Machine.exec_build(args[0]));
Special.check_number(value);
Object spec = Machine.deref(Machine.exec_build(args[1]));
Special.check_atom(spec);
Object alpha = Machine.deref(Machine.exec_build(args[2]));
Special.check_integer(alpha);
if (ComputeCompare.integer_signum((Number) alpha) < 0)
throw Machine.make_error(new Store.Compound("domain_error",
new Object[]{"not_less_than_zero", alpha}));
if (Special.is_bigint(alpha))
throw Machine.make_error(new Store.Compound("representation_error",
new Object[]{"int"}));
int digs = ((Integer)alpha).intValue();
String res;
value = value instanceof Double ? value :
Double.valueOf(ComputeElem.narrow_float((Number)value));
if ("f".equals(spec)) {
res = String.format(Locale.UK, "%." + Integer.toString(digs) + "f", value);
res = shapeFloat(res, 0);
} else if ("e".equals(spec)) {
res = String.format(Locale.UK, "%." + Integer.toString(digs) + "e", value);
res = shapeFloat(res, 0);
} else if ("g".equals(spec)) {
res = String.format(Locale.UK, "%." + Integer.toString(digs) + "g", value);
res = shapeFloat(res, 2);
} else {
throw Machine.make_error(new Store.Compound("domain_error",
new Object[]{"illegal_format", spec}));
}
return Machine.exec_unify(args[3], res);
}
public static String shapeFloat(String res, int flag) {
int peek = res.indexOf('e');
if (peek != -1) {
res = shapeFloatMantissa(res.substring(0, peek), flag) +
"e" + shapeFloatExponent(res.substring(peek + 1));
} else {
res = shapeFloatMantissa(res, flag);
}
return res;
}
private static String shapeFloatMantissa(String res, int flag) {
if ((flag & 2) != 0) {
if (res.indexOf('.') != -1) {
int pos = res.length();
while (res.charAt(pos - 1) == '0')
pos--;
if (res.charAt(pos - 1) == '.')
pos--;
if (pos != res.length())
res = res.substring(0, pos);
}
}
if ((flag & 1) != 0) {
if (res.indexOf('.') == -1)
res += ".0";
}
return res;
}
private static String shapeFloatExponent(String res) {
if (0 < res.length() && res.charAt(0) == '+')
res = res.substring(1);
if (res.startsWith("0") && 1 < res.length())
res = res.substring(1);
else if (res.startsWith("-0") && 2 < res.length())
res = "-" + res.substring(2);
return res;
}
public static void main() {
Store.add("sys_float_atom", 4, Special.make_check(strlib::test_sys_float_atom));
}
}