Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New str function #563

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions internal/c/libqb/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ libqb-objs-y += $(PATH_LIBQB)/src/math.o
libqb-objs-y += $(PATH_LIBQB)/src/shell.o
libqb-objs-y += $(PATH_LIBQB)/src/qbs.o
libqb-objs-y += $(PATH_LIBQB)/src/qbs_str.o
libqb-objs-y += $(PATH_LIBQB)/src/qbs__str.o
libqb-objs-y += $(PATH_LIBQB)/src/qbs_cmem.o
libqb-objs-y += $(PATH_LIBQB)/src/qbs_mk_cv.o
libqb-objs-y += $(PATH_LIBQB)/src/string_functions.o
Expand Down
14 changes: 14 additions & 0 deletions internal/c/libqb/include/qbs.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ qbs *qbs_set(qbs *, qbs *);

void qbs_free(qbs *str);

// legacy STR$ function prototypes
qbs *qbs_str(int64_t value);
qbs *qbs_str(int32_t value);
qbs *qbs_str(int16_t value);
Expand All @@ -52,6 +53,19 @@ qbs *qbs_str(float value);
qbs *qbs_str(double value);
qbs *qbs_str(long double value);

// modern _STR$ function prototypes
qbs *qbs__str(int64_t value);
qbs *qbs__str(int32_t value);
qbs *qbs__str(int16_t value);
qbs *qbs__str(int8_t value);
qbs *qbs__str(uint64_t value);
qbs *qbs__str(uint32_t value);
qbs *qbs__str(uint16_t value);
qbs *qbs__str(uint8_t value);
qbs *qbs__str(float value);
qbs *qbs__str(double value);
qbs *qbs__str(long double value);

qbs *func_chr(int32_t value);

qbs *qbs_ucase(qbs *str);
Expand Down
122 changes: 122 additions & 0 deletions internal/c/libqb/src/qbs__str.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@

#include "libqb-common.h"

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "error_handle.h"
#include "qbs.h"

// modern _STR() functions (no leading space and no QB4.5 compatible rounding)
// signed integers
qbs *qbs__str(int64_t value) {
qbs *tqbs = qbs_new(32, 1);
tqbs->len = snprintf((char *)tqbs->chr, 32, "%" PRId64, value);
if (tqbs->len < 0 || tqbs->len >= 32) {
error(QB_ERROR_INTERNAL_ERROR);
tqbs->len = 0;
}
return tqbs;
}
qbs *qbs__str(int32_t value) {
qbs *tqbs = qbs_new(32, 1);
tqbs->len = snprintf((char *)tqbs->chr, 32, "%i", value);
if (tqbs->len < 0 || tqbs->len >= 32) {
error(QB_ERROR_INTERNAL_ERROR);
tqbs->len = 0;
}
return tqbs;
}
qbs *qbs__str(int16_t value) {
qbs *tqbs = qbs_new(32, 1);
tqbs->len = snprintf((char *)tqbs->chr, 32, "%i", value);
if (tqbs->len < 0 || tqbs->len >= 32) {
error(QB_ERROR_INTERNAL_ERROR);
tqbs->len = 0;
}
return tqbs;
}
qbs *qbs__str(int8_t value) {
qbs *tqbs = qbs_new(32, 1);
tqbs->len = snprintf((char *)tqbs->chr, 32, "%i", value);
if (tqbs->len < 0 || tqbs->len >= 32) {
error(QB_ERROR_INTERNAL_ERROR);
tqbs->len = 0;
}
return tqbs;
}
// unsigned integers
qbs *qbs__str(uint64_t value) {
qbs *tqbs = qbs_new(32, 1);
tqbs->len = snprintf((char *)tqbs->chr, 32, "%" PRIu64, value);
if (tqbs->len < 0 || tqbs->len >= 32) {
error(QB_ERROR_INTERNAL_ERROR);
tqbs->len = 0;
}
return tqbs;
}
qbs *qbs__str(uint32_t value) {
qbs *tqbs = qbs_new(32, 1);
tqbs->len = snprintf((char *)tqbs->chr, 32, "%u", value);
if (tqbs->len < 0 || tqbs->len >= 32) {
error(QB_ERROR_INTERNAL_ERROR);
tqbs->len = 0;
}
return tqbs;
}
qbs *qbs__str(uint16_t value) {
qbs *tqbs = qbs_new(32, 1);
tqbs->len = snprintf((char *)tqbs->chr, 32, "%u", value);
if (tqbs->len < 0 || tqbs->len >= 32) {
error(QB_ERROR_INTERNAL_ERROR);
tqbs->len = 0;
}
return tqbs;
}
qbs *qbs__str(uint8_t value) {
qbs *tqbs = qbs_new(32, 1);
tqbs->len = snprintf((char *)tqbs->chr, 32, "%u", value);
if (tqbs->len < 0 || tqbs->len >= 32) {
error(QB_ERROR_INTERNAL_ERROR);
tqbs->len = 0;
}
return tqbs;
}
// floating points
qbs *qbs__str(float value) {
qbs *tqbs = qbs_new(32, 1);
tqbs->len = snprintf((char *)tqbs->chr, 32, "%.7G", value);
if (tqbs->len < 0 || tqbs->len >= 32) {
error(QB_ERROR_INTERNAL_ERROR);
tqbs->len = 0;
}
return tqbs;
}
qbs *qbs__str(double value) {
qbs *tqbs = qbs_new(32, 1);
tqbs->len = snprintf((char *)tqbs->chr, 32, "%.16G", value);
if (tqbs->len < 0 || tqbs->len >= 32) {
error(QB_ERROR_INTERNAL_ERROR);
tqbs->len = 0;
}
char *ex = strrchr((char*)tqbs->chr, (int)'E');
if (ex != NULL) ex[0] = 'D';
return tqbs;
}
qbs *qbs__str(long double value) {
qbs *tqbs = qbs_new(32, 1);
#ifdef QB64_MINGW
tqbs->len = __mingw_snprintf((char *)tqbs->chr, 32, "%.19LG", value);
#else
tqbs->len = snprintf((char *)tqbs->chr, 32, "%.19LG", value);
#endif
if (tqbs->len < 0 || tqbs->len >= 32) {
error(QB_ERROR_INTERNAL_ERROR);
tqbs->len = 0;
}
char *ex = strrchr((char*)tqbs->chr, (int)'E');
if (ex != NULL) ex[0] = 'F';
return tqbs;
}
9 changes: 7 additions & 2 deletions internal/c/libqb/src/qbs_str.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ qbs *qbs_str(double value) {
}

qbs *qbs_str(long double value) {
// not fully implemented
return qbs_str((double)value);
// Not available in QB4.5, so we don't need to care about compatible
// rounding and rather forward to the modern _STR$() instead.
qbs *tqbs = qbs__str(value);
if (tqbs->chr[0] != 45) { // positive?, then just prepend a space
return qbs_add(qbs_new_txt(" "), tqbs);
}
return tqbs;
}
23 changes: 0 additions & 23 deletions internal/support/color/color32.bi
Original file line number Diff line number Diff line change
Expand Up @@ -274,26 +274,3 @@ CONST Yellow~& = 4294967040
CONST YellowGreen~& = 4288335154
CONST YellowOrange~& = 4294946370

'Color constants for text mode.
'Just in case someone really needs both color sets in the same program.
'These are equal to $COLOR:0 constants, but prefixed with "z" (for zero)
'to avoid conflicts with same named constants in the block above.

CONST zBlack~%% = 0
CONST zBlue~%% = 1
CONST zGreen~%% = 2
CONST zCyan~%% = 3
CONST zRed~%% = 4
CONST zMagenta~%% = 5
CONST zBrown~%% = 6
CONST zWhite~%% = 7
CONST zGray~%% = 8
CONST zLightBlue~%% = 9
CONST zLightGreen~%% = 10
CONST zLightCyan~%% = 11
CONST zLightRed~%% = 12
CONST zLightMagenta~%% = 13
CONST zYellow~%% = 14
CONST zBrightWhite~%% = 15
CONST zBlink~%% = 16

11 changes: 11 additions & 0 deletions source/subs_functions/subs_functions.bas
Original file line number Diff line number Diff line change
Expand Up @@ -3292,6 +3292,17 @@ SUB reginternal
id.hr_syntax = "STR$(number)"
regid

clearid
id.n = "_Str"
id.musthave = "$"
id.subfunc = 1
id.callname = "qbs__str"
id.args = 1
id.arg = MKL$(-1)
id.ret = STRINGTYPE - ISPOINTER
id.hr_syntax = "_STR$(number)"
regid

clearid
id.n = "UCase"
id.musthave = "$"
Expand Down
2 changes: 1 addition & 1 deletion source/subs_functions/syntax_highlighter_list.bas
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ listOfKeywords$ = listOfKeywords$ +_

' [S] - Keywords alphabetical (1st line = QB64, 2nd line = QB4.5, 3rd line = OpenGL)
listOfKeywords$ = listOfKeywords$ +_
"_SAVEFILEDIALOG$@_SAVEIMAGE@_SCALEDHEIGHT@_SCALEDWIDTH@_SCREENCLICK@_SCREENEXISTS@_SCREENHIDE@_SCREENICON@_SCREENIMAGE@_SCREENMOVE@_SCREENPRINT@_SCREENSHOW@_SCREENX@_SCREENY@_SCROLLLOCK@_SEAMLESS@_SEC@_SECH@_SELECTFOLDERDIALOG$@_SETALPHA@_SETBIT@_SHELLHIDE@_SHL@_SHOW@_SHR@_SINH@_SMOOTH@_SMOOTHSHRUNK@_SMOOTHSTRETCHED@_SNDBAL@_SNDCLOSE@_SNDCOPY@_SNDGETPOS@_SNDLEN@_SNDLIMIT@_SNDLOOP@_SNDNEW@_SNDOPEN@_SNDOPENRAW@_SNDPAUSE@_SNDPAUSED@_SNDPLAY@_SNDPLAYCOPY@_SNDPLAYFILE@_SNDPLAYING@_SNDRATE@_SNDRAW@_SNDRAWDONE@_SNDRAWLEN@_SNDSETPOS@_SNDSTOP@_SNDVOL@_SOFTWARE@_SOURCE@_SQUAREPIXELS@_STARTDIR$@_STATUSCODE@_STRCMP@_STRETCH@_STRICMP@" +_
"_SAVEFILEDIALOG$@_SAVEIMAGE@_SCALEDHEIGHT@_SCALEDWIDTH@_SCREENCLICK@_SCREENEXISTS@_SCREENHIDE@_SCREENICON@_SCREENIMAGE@_SCREENMOVE@_SCREENPRINT@_SCREENSHOW@_SCREENX@_SCREENY@_SCROLLLOCK@_SEAMLESS@_SEC@_SECH@_SELECTFOLDERDIALOG$@_SETALPHA@_SETBIT@_SHELLHIDE@_SHL@_SHOW@_SHR@_SINH@_SMOOTH@_SMOOTHSHRUNK@_SMOOTHSTRETCHED@_SNDBAL@_SNDCLOSE@_SNDCOPY@_SNDGETPOS@_SNDLEN@_SNDLIMIT@_SNDLOOP@_SNDNEW@_SNDOPEN@_SNDOPENRAW@_SNDPAUSE@_SNDPAUSED@_SNDPLAY@_SNDPLAYCOPY@_SNDPLAYFILE@_SNDPLAYING@_SNDRATE@_SNDRAW@_SNDRAWDONE@_SNDRAWLEN@_SNDSETPOS@_SNDSTOP@_SNDVOL@_SOFTWARE@_SOURCE@_SQUAREPIXELS@_STARTDIR$@_STATUSCODE@_STR$@_STRCMP@_STRETCH@_STRICMP@" +_
"SADD@SCREEN@SEEK@SEG@SELECT@SETMEM@SGN@SHARED@SHELL@SIGNAL@SIN@SINGLE@SLEEP@SMOOTH@SOUND@SPACE$@SPC@SQR@STATIC@STEP@STICK@STOP@STR$@STRETCH@STRIG@STRING@STRING$@SUB@SWAP@SYSTEM@" +_
"_GLSCALED@_GLSCALEF@_GLSCISSOR@_GLSELECTBUFFER@_GLSHADEMODEL@_GLSTENCILFUNC@_GLSTENCILMASK@_GLSTENCILOP@"

Expand Down
17 changes: 17 additions & 0 deletions tests/compile_tests/new_str/strTestConst.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
$CONSOLE:ONLY

PRINT
PRINT "("; _STR$(_SINGLE_MIN); ")"
PRINT "("; _STR$(_SINGLE_MAX); ")"
PRINT
PRINT "("; _STR$(_DOUBLE_MIN); ")"
PRINT "("; _STR$(_DOUBLE_MAX); ")"
PRINT
PRINT "("; _STR$(_FLOAT_MIN); ")"
PRINT "("; _STR$(_FLOAT_MAX); ")"
PRINT
PRINT "("; STR$(_FLOAT_MIN); ")"
PRINT "("; STR$(_FLOAT_MAX); ")"

SYSTEM

12 changes: 12 additions & 0 deletions tests/compile_tests/new_str/strTestConst.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

(-3.402823E+38)
(3.402823E+38)

(-1.797693134862315D+308)
(1.797693134862315D+308)

(-1.189731495357231765F+4932)
(1.189731495357231765F+4932)

(-1.189731495357231765F+4932)
( 1.189731495357231765F+4932)
17 changes: 17 additions & 0 deletions tests/compile_tests/new_str/strTestLiteral.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
$CONSOLE:ONLY

PRINT
PRINT "("; _STR$(-3.402823E+38); ")"
PRINT "("; _STR$(3.402823E+38); ")"
PRINT
PRINT "("; _STR$(-1.797693134862315D+308); ")"
PRINT "("; _STR$(1.797693134862315D+308); ")"
PRINT
PRINT "("; _STR$(-1.189731495357231765F+4932); ")"
PRINT "("; _STR$(1.189731495357231765F+4932); ")"
PRINT
PRINT "("; STR$(-1.189731495357231765F+4932); ")"
PRINT "("; STR$(1.189731495357231765F+4932); ")"

SYSTEM

12 changes: 12 additions & 0 deletions tests/compile_tests/new_str/strTestLiteral.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

(-3.402823E+38)
(3.402823E+38)

(-1.797693134862315D+308)
(1.797693134862315D+308)

(-1.189731495357231765F+4932)
(1.189731495357231765F+4932)

(-1.189731495357231765F+4932)
( 1.189731495357231765F+4932)
23 changes: 23 additions & 0 deletions tests/compile_tests/new_str/strTestVariable.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
$CONSOLE:ONLY

smi! = -3.402823E+38
sma! = 3.402823E+38
dmi# = -1.797693134862315D+308
dma# = 1.797693134862315D+308
fmi## = -1.189731495357231765F+4932
fma## = 1.189731495357231765F+4932
PRINT
PRINT "("; _STR$(smi!); ")"
PRINT "("; _STR$(sma!); ")"
PRINT
PRINT "("; _STR$(dmi#); ")"
PRINT "("; _STR$(dma#); ")"
PRINT
PRINT "("; _STR$(fmi##); ")"
PRINT "("; _STR$(fma##); ")"
PRINT
PRINT "("; STR$(fmi##); ")"
PRINT "("; STR$(fma##); ")"

SYSTEM

12 changes: 12 additions & 0 deletions tests/compile_tests/new_str/strTestVariable.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

(-3.402823E+38)
(3.402823E+38)

(-1.797693134862315D+308)
(1.797693134862315D+308)

(-1.189731495357231765F+4932)
(1.189731495357231765F+4932)

(-1.189731495357231765F+4932)
( 1.189731495357231765F+4932)
Loading