Java "strlib"

Admin User, created Apr 26. 2025
         
package foreign.util;
import nova.Machine;
import nova.special;
import nova.Store;
import nova.envir.ComputeCompare;
import nova.envir.ComputeElem;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
/**
* Foreign functions for library(util/format)
* <p/>
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
* <p/>
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
* <p/>
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
* <p/>
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
* <p/>
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
public final class strlib {
/*******************************************************************/
/* Number Formatting */
/*******************************************************************/
/**
* sys_float_atom(F, S, N, A):
* The built-in succeeds in A with the number F
* formatted according to the format specifier S
* and the number of digits N.
*/
private static boolean test_sys_float_atom(Object[] args) {
Object num = Machine.deref(Machine.exec_build(args[0]));
special.check_number(num);
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 text;
double value = num instanceof Double ? ((Double) num).doubleValue() :
ComputeElem.narrow_float((Number) num);
if ("f".equals(spec)) {
text = floatAtom(value, digs, "0.0");
text = shapeFloat(text, 0);
} else if ("e".equals(spec)) {
text = floatAtom(value, digs, "0.0E0");
text = shapeFloat(text, 0);
} else if ("g".equals(spec)) {
int mag = (value != 0 ? (int)Math.floor(Math.log(
Math.abs(value)) / Math.log(10)) : 0);
if (mag >= digs || mag < -4) {
text = floatAtom(value, digs - 1, "0.0E0");
} else {
text = floatAtom(value, digs - 1 - mag, "0.0");
}
text = shapeFloat(text, 2);
} else {
throw Machine.make_error(new Store.Compound("existence_error",
new Object[]{"format_character", spec}));
}
return Machine.exec_unify(args[3], text);
}
private static String floatAtom(double value, int digs, String type) {
DecimalFormat fmt = (DecimalFormat)NumberFormat.getInstance(Locale.UK);
fmt.applyPattern(type);
fmt.setMaximumFractionDigits(digs);
fmt.setMinimumFractionDigits(digs);
fmt.setRoundingMode(RoundingMode.HALF_EVEN);
fmt.setGroupingUsed(false);
return fmt.format(value);
}
/**
* Shape the number string so that it has lower case exponent.
* If (flag & 2) !== 0 remove trailing zeros, with removing
* the period, from mantissa.
*
* @param res The ascii number string.
* @param flag The shaping flags.
* @return The shaped number string.
*/
public static String shapeFloat(String res, int flag) {
int peek = res.indexOf('E');
if (peek != -1) {
res = shapeFloatMantissa(res.substring(0, peek), flag) +
"e" + 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);
}
}
return res;
}
/*********************************************************************/
/* Str Lib Init */
/*********************************************************************/
public static void main() {
Store.add("sys_float_atom", 4, special.make_check(strlib::test_sys_float_atom));
}
}