Skip to content

Commit

Permalink
ADDED: write_term/2: option radix(Radix).
Browse files Browse the repository at this point in the history
Experimental.  Under discussion as PIP-105.  The current implementation
writes, unlike XSB, the radix prefix.  We either need more options
or we should consider another alternative such as integer_format(+Format)

As this patch cleans up a lot of the code regarding numeric output and
most will be needed anyway, it is merged despite the fact that the
radix option may vanish eventually.
  • Loading branch information
JanWielemaker committed Nov 15, 2024
1 parent 78ee873 commit 841a487
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 111 deletions.
2 changes: 2 additions & 0 deletions src/ATOMS
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ A heap_gc "heap_gc"
A heapused "heapused"
A heartbeat "heartbeat"
A help "help"
A hex "hex"
A hidden "hidden"
A hide_childs "hide_childs"
A history_depth "history_depth"
Expand Down Expand Up @@ -594,6 +595,7 @@ A numbervars "numbervars"
A numerator "numerator"
A obfuscate "obfuscate"
A occurs_check "occurs_check"
A octal "octal"
A octet "octet"
A off "off"
A offset "offset"
Expand Down
35 changes: 27 additions & 8 deletions src/os/pl-fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@ source should also use format() to produce error messages, etc.
#include <stdio.h>
#include <math.h>
#include <fenv.h>
#include "pl-fmt.h"
#ifdef __WINDOWS__
#include "../pl-nt.h"
#endif

typedef foreign_t (*Func1)(term_t a1);

static char * formatInteger(PL_locale *locale, int div, int radix,
bool smll, Number n, Buffer out);
static char * formatFloat(PL_locale *locale, int how, int arg,
Number f, Buffer out);

Expand Down Expand Up @@ -1052,7 +1051,15 @@ revert_string(char *s, size_t len)
}
}

static char *
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Format the integer `i` to the buffer `out`. `div` is for supporting
fixed point numbers. `radix` is the base and `smll` defines whether
to use capitals (`false`) or lowercase letters for digit values above
9.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */


char *
formatInteger(PL_locale *locale, int div, int radix, bool smll, Number i,
Buffer out)
{ const char *grouping = NULL;
Expand Down Expand Up @@ -1725,12 +1732,24 @@ formatFloat(PL_locale *locale, int how, int arg, Number f, Buffer out)
int size = 0;

if ( how == 'h' || how == 'H' )
{ if ( !growBuffer(out, 100) )
{ PL_no_memory();
return NULL;
{ size_t space = 32;

for(int n=0; n<2; n++)
{ size_t sz;

if ( !growBuffer(out, space) )
{ PL_no_memory();
return NULL;
}
sz = format_float(out->base, space, f->value.f,
arg, how == 'H' ? 'E' : 'e');
if ( sz < space )
{ written = sz;
break;
} else
{ space = sz+1;
}
}
format_float(f->value.f, arg, how == 'H' ? 'E' : 'e', out->base);
written = strlen(out->base);
} else
{ Ssprintf(tmp, "%%.%d%c", arg, how);
while(written >= size)
Expand Down
43 changes: 43 additions & 0 deletions src/os/pl-fmt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2024, University of Amsterdam
VU University Amsterdam
CWI, Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef FMT_H_INCLUDED
#define FMT_H_INCLUDED

COMMON(char *) formatInteger(PL_locale *locale, int div, int radix,
bool smll, Number n, Buffer out);

#endif /*FMT_H_INCLUDED*/
20 changes: 16 additions & 4 deletions src/os/pl-text.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,23 @@ PL_get_text(DECL_LD term_t l, PL_chars_t *text, int flags)
text->encoding = ENC_ISO_LATIN_1;
text->canonical = true;
} else if ( (flags & CVT_FLOAT) && isFloat(w) )
{ format_float(valFloat(w), 3, 'e', text->buf);
text->text.t = text->buf;
text->length = strlen(text->text.t);
{ size_t sz = format_float(text->buf, sizeof(text->buf),
valFloat(w), 3, 'e');
if ( sz < sizeof(text->buf) )
{ text->text.t = text->buf;
text->length = sz;
text->storage = PL_CHARS_LOCAL;
} else
{ Buffer b = findBuffer(BUF_STACK);

if ( !growBuffer(b, sz+1) )
outOfCore();
format_float(b->base, sz+1, valFloat(w), 3, 'e');
text->text.t = baseBuffer(b, char);
text->length = sz;
text->storage = PL_CHARS_STACK;
}
text->encoding = ENC_ISO_LATIN_1;
text->storage = PL_CHARS_LOCAL;
text->canonical = true;
} else if ( (flags & CVT_LIST) )
{ Buffer b;
Expand Down
9 changes: 8 additions & 1 deletion src/pl-fli.c
Original file line number Diff line number Diff line change
Expand Up @@ -787,14 +787,21 @@ static int compareUCSAtom(atom_t h1, atom_t h2);
static int saveUCSAtom(atom_t a, IOSTREAM *fd);
static atom_t loadUCSAtom(IOSTREAM *fd);

static int
blob_write_usc_atom(IOSTREAM *fd, atom_t atom, int flags)
{ bool rc = writeUCSAtom(fd, atom, flags);

return rc ? 1 : -1;
}

static PL_blob_t ucs_atom =
{ PL_BLOB_MAGIC,
PL_BLOB_UNIQUE|PL_BLOB_TEXT|PL_BLOB_WCHAR,
/* unique representation of text */
"ucs_text",
NULL, /* release */
compareUCSAtom, /* compare */
writeUCSAtom, /* write */
blob_write_usc_atom, /* write */
NULL, /* acquire */
saveUCSAtom, /* save load to/from .qlf files */
loadUCSAtom
Expand Down
12 changes: 10 additions & 2 deletions src/pl-ressymbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2013-2017, VU University Amsterdam
Copyright (c) 2013-2024, VU University Amsterdam
SWI-Prolog Solutions b.v.
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -43,14 +44,21 @@

static int compareReservedSymbol(atom_t h1, atom_t h2);

static int
blob_write_reserved_symbol(IOSTREAM *fd, atom_t atom, int flags)
{ bool rc = writeReservedSymbol(fd, atom, flags);

return rc ? 1 : -1;
}

static PL_blob_t reserved_symbol =
{ PL_BLOB_MAGIC,
PL_BLOB_UNIQUE,
/* unique representation of text */
"reserved_symbol",
NULL, /* release */
compareReservedSymbol, /* compare */
writeReservedSymbol, /* write */
blob_write_reserved_symbol, /* write */
NULL, /* acquire */
NULL, /* save load to/from .qlf files */
NULL,
Expand Down
Loading

0 comments on commit 841a487

Please sign in to comment.