Next: , Previous: Single-key input, Up: Other I/O


5.19.7 Line input and conversion

For ways of storing character strings in memory see String Formats.

Words for inputting one line from the keyboard:

accept       c-addr +n1 – +n2         core       “accept”

Get a string of up to n1 characters from the user input device and store it at c-addr. n2 is the length of the received string. The user indicates the end by pressing <RET>. Gforth supports all the editing functions available on the Forth command line (including history and word completion) in accept.

edit-line       c-addr n1 n2 – n3         gforth       “edit-line”

edit the string with length n2 in the buffer c-addr n1, like accept.

Conversion words:

s>number?       addr u – d f         gforth       “s>number?”

converts string addr u into d, flag indicates success

s>unumber?       c-addr u – ud flag         gforth       “s>unumber?”

converts string c-addr u into ud, flag indicates success

>number       ud1 c-addr1 u1 – ud2 c-addr2 u2         core       “to-number”

Attempt to convert the character string c-addr1 u1 to an unsigned number in the current number base. The double ud1 accumulates the result of the conversion to form ud2. Conversion continues, left-to-right, until the whole string is converted or a character that is not convertable in the current number base is encountered (including + or -). For each convertable character, ud1 is first multiplied by the value in BASE and then incremented by the value represented by the character. c-addr2 is the location of the first unconverted character (past the end of the string if the whole string was converted). u2 is the number of unconverted characters in the string. Overflow is not detected.

>float       c-addr u – f:... flag        float       “to-float”

Actual stack effect: ( c_addr u – r t | f ). Attempt to convert the character string c-addr u to internal floating-point representation. If the string represents a valid floating-point number r is placed on the floating-point stack and flag is true. Otherwise, flag is false. A string of blanks is a special case and represents the floating-point number 0.

Obsolescent input and conversion words:

convert       ud1 c-addr1 – ud2 c-addr2         core-ext-obsolescent       “convert”

Obsolescent: superseded by >number.

expect       c-addr +n –         core-ext-obsolescent       “expect”

Receive a string of at most +n characters, and store it in memory starting at c-addr. The string is displayed. Input terminates when the <return> key is pressed or +n characters have been received. The normal Gforth line editing capabilites are available. The length of the string is stored in span; it does not include the <return> character. OBSOLESCENT: superceeded by accept.

span       – c-addr         core-ext-obsolescent       “span”

Variablec-addr is the address of a cell that stores the length of the last string received by expect. OBSOLESCENT.