Guess the data type that a string represents.
DataType_Type _slang_guess_type (String_Type s)
This function tries to determine whether its argument s
represents
an integer or a floating point number. If it appears to be neither,
then a string is assumed. It returns one of three values depending on
the format of the string s
:
Integer_Type : If it appears to be an integer
Double_Type : If it appears to be a double
String_Type : Anything else.
For example, _slang_guess_type("1e2")
returns
Double_Type
but _slang_guess_type("e12")
returns
String_Type
.
integer, string, double
Get the data type of an object
DataType_Type _typeof (x)
This function is similar to the typeof
function except in the
case of arrays. If the object x
is an array, then the data
type of the array will be returned. otherwise _typeof
returns
the data type of x
.
if (Integer_Type == _typeof (x))
message ("x is an integer or an integer array");
typeof, array_info, _slang_guess_type, typecast
Convert a string to a double precision number
Double_Type atof (String_Type s)
This function converts a string s
to a double precision value
and returns the result. It performs no error checking on the format
of the string. The function _slang_guess_type
may be used to
check the syntax of the string.
define error_checked_atof (s)
{
switch (_slang_guess_type (s))
{
case Double_Type:
return atof (s);
}
{
case Integer_Type:
return double (integer (s));
}
verror ("%s is is not a double", s);
}
typecast, double, _slang_guess_type
Convert an ascii value into a string
String_Type char (Integer_Type c)
The char
function converts an integer ascii value c
to a string
of unit length such that the first character of the string is c
.
For example, char('a')
returns the string "a"
.
integer, string, typedef
Define upper-lower case conversion.
define_case (Integer_Type ch_up, Integer_Type ch_low);
This function defines an upper and lowercase relationship between two
characters specified by the arguments. This relationship is used by
routines which perform uppercase and lowercase conversions.
The first integer ch_up
is the ascii value of the uppercase character
and the second parameter ch_low
is the ascii value of its
lowercase counterpart.
strlow, strup
Convert an object to double precision
result = double (x)
The double
function typecasts an object x
to double
precision. For example, if x
is an array of integers, an
array of double types will be returned. If an object cannot be
converted to Double_Type
, a type-mismatch error will result.
The double
function is equivalent to the typecast operation
typecast (x, Double_Type)
To convert a string to a double precision number, use atoi
function.
typecast, atoi, int
Typecast an object to an integer
int (s)
This function performs a typecast of s
from its data type to
an object of Integer_Type
. If s
is a string, it returns
returns the ascii value value of the first character of the string
s
. If s
is Double_Type
, int
truncates the
number to an integer and returns it.
int
can be used to convert single character strings to
integers. As an example, the intrinsic function isdigit
may
be defined as
define isdigit (s)
{
if ((int (s) >= '0') and (int (s) <= '9')) return 1;
return 0;
}
This function is equalent to typecast (s, Integer_Type)
;
typecast, double, integer, char, isdigit
Convert a string to an integer
Integer_Type integer (String_Type s)
The integer
function converts a string representation of an
integer back to an integer. If the string does not form a valid
integer, a type-mismatch error will be generated.
integer ("1234")
returns the integer value 1234
.
This function operates only on strings and is not the same as the
more general typecast
operator.
typecast, _slang_guess_type, string, sprintf, char
Tests for a decimal digit character
Integer_Type isdigit (String_Type s)
This function returns a non-zero value if the first character in the
string s
is a digit; otherwise, it returns zero.
A simple, user defined implementation of isdigit
is
define isdigit (s)
{
return ((s[0] <= '9') and (s[0] >= '0'));
}
However, the intrinsic function isdigit
executes many times faster
than the equivalent representation defined above.
Unlike the C function with the same name, the S-lang function takes a string argument.
int, integer
Convert an object to a string representation.
Integer_Type string (obj)
The string
function may be used to convert an object
obj
of any type to a string representation.
For example, string(12.34)
returns "12.34"
.
define print_anything (anything)
{
message (string (anything));
}
This function is not the same as typecasting to a String_Type
using the typecast
function.
typecast, sprintf, integer, char
Convert a character to lowercase.
Integer_Type lower (Integer_Type ch)
This function takes an integer ch
and returns its lowercase
equivalent.
toupper, strup, strlow, int, char, define_case
Convert a character to uppercase.
Integer_Type toupper (Integer_Type ch)
This function takes an integer ch
and returns its uppercase
equivalent.
tolower, strup, strlow, int, char, define_case
Convert an object from one data type to another.
typecast (x, new_type)
The typecast
function performs a generic typecast operation on
x
to convert it to new_type
. If x
represents an
array, the function will attempt to convert all elements of x
to new_type
. Not all objects can be converted and a
type-mismatch error will result upon failure.
define to_complex (x)
{
return typecast (x, Complex_Type);
}
defines a function that converts its argument, x
to a complex
number.
int, double, typeof
Get the data type of an object.
DataType_Type typeof (x)
This function returns the data type of x
.
if (Integer_Type == typeof (x)) message ("x is an integer");
_typeof, is_struct_type, array_info, _slang_guess_type, typecast