Skip to content

Commit

Permalink
Rename Qualify_String, factor out scan constants
Browse files Browse the repository at this point in the history
The routine Qualify_String had a cryptic name, and some instances
would comment what it did...warning readers that it could raise errors
and that the buffer it returned was temporary.  Those two facts were
much more important in the name than "Qualify" or even "String", so
this renames it to `Temp_Byte_Chars_May_Fail` and expands upon
the comments for the reason for its existence.

There were also several hardcoded constants used with this routine
to make buffers such that they would align with the maximum length
of a scanned input for a type.  This puts all those numbers together
as constants in sys-scan.h so that they may be considered (in
particular, how necessary it is to try and codify such limits for these
conversions, as it seems error-prone to keep in sync with the
scanner if new notations arise.)
  • Loading branch information
hostilefork committed Nov 14, 2015
1 parent ff7199f commit e03f3cf
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 25 deletions.
1 change: 0 additions & 1 deletion src/core/f-enbase.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
***********************************************************************/

#include "sys-core.h"
#include "sys-scan.h"


/***********************************************************************
Expand Down
1 change: 0 additions & 1 deletion src/core/l-scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
***********************************************************************/

#include "sys-core.h"
#include "sys-scan.h"

// In UTF8 C0, C1, F5, and FF are invalid.
#ifdef USE_UNICODE
Expand Down
1 change: 0 additions & 1 deletion src/core/l-types.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
***********************************************************************/

#include "sys-core.h"
#include "sys-scan.h"
#include "sys-deci-funcs.h"
#include "sys-dec-to-char.h"
#include <errno.h>
Expand Down
1 change: 0 additions & 1 deletion src/core/s-make.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
***********************************************************************/

#include "sys-core.h"
#include "sys-scan.h"


/***********************************************************************
Expand Down
1 change: 0 additions & 1 deletion src/core/s-mold.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

//#define INCLUDE_TYPE_NAMES // include the value names table
#include "sys-core.h"
#include "sys-scan.h"
#include <float.h>

#define MAX_QUOTED_STR 50 // max length of "string" before going to { }
Expand Down
16 changes: 8 additions & 8 deletions src/core/s-ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
***********************************************************************/

#include "sys-core.h"
#include "sys-scan.h"


/*********************************************************************
Expand Down Expand Up @@ -64,10 +63,16 @@

/*********************************************************************
**
*/ REBYTE *Qualify_String(const REBVAL *val, REBINT max_len, REBCNT *length, REBINT opts)
*/ REBYTE *Temp_Byte_Chars_May_Fail(const REBVAL *val, REBINT max_len, REBCNT *length, REBINT opts)
/*
** NOTE: This function returns a temporary result, and uses an internal
** buffer. Do not use it recursively. Also, it will Trap on errors.
**
** Prequalifies a string before using it with a function that
** expects it to be 8-bits.
** expects it to be 8-bits. It would be used for instance to convert
** a string that is potentially REBUNI-wide into a form that can be used
** with a Scan_XXX routine, that is expecting ASCII or UTF-8 source.
** (Many TO-XXX conversions from STRING re-use that scanner logic.)
**
** Returns a temporary string and sets the length field.
**
Expand All @@ -84,11 +89,6 @@
** 4. it does not contain other values ("123 456")
** 5. it's not empty or only whitespace
**
** Notes:
*
** 1. This function will TRAP on errors.
** 2. Do not recursively use it (internal buffer)
**
***********************************************************************/
{
REBCNT tail = VAL_TAIL(val);
Expand Down
3 changes: 1 addition & 2 deletions src/core/t-date.c
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,7 @@
if (IS_STRING(arg)) {
REBYTE *bp;
REBCNT len;
// 30-September-10000/12:34:56.123456789AM/12:34
bp = Qualify_String(arg, 45, &len, FALSE); // can trap, ret diff str
bp = Temp_Byte_Chars_May_Fail(arg, MAX_SCAN_DATE, &len, FALSE);
if (Scan_Date(bp, len, D_OUT)) return R_OUT;
}
else if (ANY_ARRAY(arg) && VAL_BLK_LEN(arg) >= 3) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/t-decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ REBOOL almost_equal(REBDEC a, REBDEC b, REBCNT max_diff) {
{
REBYTE *bp;
REBCNT len;
bp = Qualify_String(val, 24, &len, FALSE);
bp = Temp_Byte_Chars_May_Fail(val, MAX_SCAN_DECIMAL, &len, FALSE);

VAL_SET(D_OUT, REB_DECIMAL);
if (Scan_Decimal(
Expand All @@ -389,7 +389,7 @@ REBOOL almost_equal(REBDEC a, REBDEC b, REBCNT max_diff) {
{
REBYTE *bp;
REBCNT len;
bp = Qualify_String(val, MAX_HEX_LEN, &len, FALSE);
bp = Temp_Byte_Chars_May_Fail(val, MAX_HEX_LEN, &len, FALSE);
if (Scan_Hex(&VAL_INT64(D_OUT), bp, len, len) == 0)
raise Error_Bad_Make(REB_DECIMAL, val);
d1 = VAL_DECIMAL(D_OUT);
Expand Down
2 changes: 1 addition & 1 deletion src/core/t-integer.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
REBYTE *bp;
REBCNT len;
REBDEC dec;
bp = Qualify_String(value, VAL_LEN(value), &len, FALSE);
bp = Temp_Byte_Chars_May_Fail(value, VAL_LEN(value), &len, FALSE);
if (
memchr(bp, '.', len)
|| memchr(bp, 'e', len)
Expand Down
2 changes: 1 addition & 1 deletion src/core/t-money.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
case REB_STRING:
{
const REBYTE *end;
str = Qualify_String(arg, 36, 0, FALSE);
str = Temp_Byte_Chars_May_Fail(arg, MAX_SCAN_MONEY, 0, FALSE);
VAL_MONEY_AMOUNT(D_OUT) = string_to_deci(str, &end);
if (end == str || *end != 0) raise Error_Bad_Make(REB_MONEY, arg);
break;
Expand Down
2 changes: 1 addition & 1 deletion src/core/t-pair.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@
REBYTE *bp;
REBCNT len;
// -1234567890x-1234567890
bp = Qualify_String(val, VAL_LEN(val), &len, FALSE);
bp = Temp_Byte_Chars_May_Fail(val, VAL_LEN(val), &len, FALSE);
if (Scan_Pair(bp, len, D_OUT)) return R_OUT;
}
if (IS_INTEGER(val)) {
Expand Down
1 change: 0 additions & 1 deletion src/core/t-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
***********************************************************************/

#include "sys-core.h"
#include "sys-scan.h"
#include "sys-deci-funcs.h"
#include "sys-int-funcs.h"

Expand Down
2 changes: 1 addition & 1 deletion src/core/t-time.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
else if (IS_STRING(val)) {
REBYTE *bp;
REBCNT len;
bp = Qualify_String(val, 30, &len, FALSE); // can trap, ret diff str
bp = Temp_Byte_Chars_May_Fail(val, MAX_SCAN_TIME, &len, FALSE);
if (!Scan_Time(bp, len, val)) goto no_time;
secs = VAL_TIME(val);
}
Expand Down
3 changes: 1 addition & 2 deletions src/core/t-tuple.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,7 @@
// URL! here fixes it, though there are still open questions.
//
if (IS_STRING(arg) || IS_URL(arg)) {
// Convert REBUNI-wide strings to byte buffer to use w/Scan_Tuple
ap = Qualify_String(arg, 11*4+1, &len, FALSE); // can trap, ret diff str
ap = Temp_Byte_Chars_May_Fail(arg, MAX_SCAN_TUPLE, &len, FALSE);
if (Scan_Tuple(ap, len, D_OUT)) return R_OUT;
goto bad_arg;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/t-word.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
REBYTE *bp;
REBCNT len;
// Set sym. Rest is set below.
bp = Qualify_String(arg, 255, &len, TRUE);
bp = Temp_Byte_Chars_May_Fail(arg, MAX_SCAN_WORD, &len, TRUE);
if (type == REB_ISSUE) sym = Scan_Issue(bp, len);
else sym = Scan_Word(bp, len);
if (!sym) raise Error_1(RE_BAD_CHAR, arg);
Expand Down
2 changes: 2 additions & 0 deletions src/include/sys-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ typedef struct rebol_mold {
#include "sys-stack.h"
#include "sys-state.h"

#include "sys-scan.h"

/***********************************************************************
**
** Constants
Expand Down
29 changes: 29 additions & 0 deletions src/include/sys-scan.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,35 @@ enum {
SCAN_MAX
};


//
// MAXIMUM LENGTHS
//
// These are the maximum input lengths in bytes needed for a buffer to give
// to Scan_XXX (not including terminator?) The TO conversions from strings
// tended to hardcode the numbers, so that hardcoding is excised here to
// make it more clear what those numbers are and what their motivation might
// have been (not all were explained).
//
// (See also MAX_HEX_LEN, MAX_INT_LEN)
//

// 30-September-10000/12:34:56.123456789AM/12:34
#define MAX_SCAN_DATE 45

// The maximum length a tuple can be in characters legally for Scan_Tuple
// (should be in a better location, but just excised it for clarity.
#define MAX_SCAN_TUPLE (11 * 4 + 1)

#define MAX_SCAN_DECIMAL 24

#define MAX_SCAN_MONEY 36

#define MAX_SCAN_TIME 30

#define MAX_SCAN_WORD 255


/*
** Externally Accessed Variables
*/
Expand Down

0 comments on commit e03f3cf

Please sign in to comment.