This chapter provides reference material for the string manager. The string manager API is declared in the header file StringMgr.h
.
For more information, see Chapter 8, "Text," of the Palm OS Programmer's Companion, vol. I.
String Manager Functions
StrAToI Function
Purpose
Convert a string to an integer.
Declared In
StringMgr.h
Prototype
Int32 StrAToI ( const Char *str )
Parameters
Returns
Comments
Use this function instead of the standard atoi
routine.
StrCaselessCompare Function
Purpose
Compare two strings with case and accent insensitivity.
Declared In
StringMgr.h
Prototype
Int16 StrCaselessCompare ( const Char *s1, const Char *s2 )
Parameters
Returns
Returns 0 if the strings match.
Returns a positive number if s1
> s2
.
Returns a negative number if s1
< s2
.
Comments
Use this function instead of the standard stricmp
routine.
To support systems that use multi-byte character encodings, consider using TxtCaselessCompare()
instead of this function (or TxtCompare()
for a case-sensitive comparison). Both functions can match single-byte characters with their multi-byte equivalents, but TxtCaselessCompare
can also return the length of the matching text.
See Also
StrNCaselessCompare()
, TxtCaselessCompare()
, StrCompare()
, StrNCompare()
StrCat Function
Purpose
Concatenate one null-terminated string to another.
Declared In
StringMgr.h
Prototype
Char *StrCat ( Char *dst, const Char *src )
Parameters
-
→
dst
- Pointer to the null-terminated destination string.
-
→
src
- Pointer to the null-terminated source string.
Returns
Returns a pointer to the destination string.
Comments
Use this function instead of the standard strcat
routine.
StrChr Function
Purpose
Look for a character within a string.
Declared In
StringMgr.h
Prototype
Char *StrChr ( const Char *str, WChar chr )
Parameters
Returns
Returns a pointer to the first occurrence of character in str
. Returns NULL
if the character is not found.
Comments
Use this function instead of the standard strchr
routine.
This function can handle both single-byte characters and multi-byte characters correctly. However, you should make sure that you pass a WChar
variable to StrChr
instead of a Char
. If you pass a Char
variable, the function sign-extends the variable to a WChar
, which causes problems if the value is 0x80 or higher.
Compatibility
This routine does not correctly find a '\0' character on Palm OS® version 1.0.
See Also
StrCompare Function
Purpose
Declared In
StringMgr.h
Prototype
Int16 StrCompare ( const Char *s1, const Char *s2 )
Parameters
Returns
Returns 0 if the strings match.
Returns a positive number if s1
sorts after s2
alphabetically.
Returns a negative number if s1
sorts before s2
alphabetically.
Comments
Use this function or StrCompareAscii()
instead of the standard strcmp
routine. This function is case sensitive.
To support systems that use multi-byte character encodings, consider using TxtCompare()
instead of this function. Both functions can match single-byte characters with their multi-byte equivalents, but TxtCompare
can also return the length of the matching text.
Compatibility
Prior to Palm OS 4.0, StrCompare
and TxtCompare()
only performed one level of comparison and returned as soon as they found two unequal characters. For example, if you compared the string "celery" with the string "Cauliflower," both functions returned a value indicating that "celery" should appear before "Cauliflower" because they sorted "c" before "C."
In Palm OS 4.0, StrCompare
calls TxtCompare
, and TxtCompare
performs a comparison using up to six comparison tables for sorting with increasing precision. As a result, in Palm OS 4.0 and higher, StrCompare
sorts "Cauliflower" before "celery."
See Also
StrNCompare()
, StrNCaselessCompare()
, TxtCaselessCompare()
StrCompareAscii Function
Purpose
Declared In
StringMgr.h
Prototype
Int16 StrCompareAscii ( const Char *s1, const Char *s2 )
Parameters
Returns
Returns 0 if the strings match.
Returns a positive number if s1
sorts after s2
alphabetically.
Returns a negative number if s1
sorts before s2
alphabetically.
Comments
Use this function instead of the standard strcmp
routine. Use it to do case-sensitive comparisons on strings that are guaranteed to be 7-bit ASCII strings. This function performs a fast, simple byte-to-byte comparison that is guaranteed never to change.
Compatibility
Implemented only if 4.0 New Feature Set is present.
See Also
StrCompare()
, StrNCompare()
, TxtCompare()
, StrCaselessCompare()
, StrNCaselessCompare()
, TxtCaselessCompare()
, StrNCompareAscii()
StrCopy Function
Purpose
Declared In
StringMgr.h
Prototype
Char *StrCopy ( Char *dst, const Char *src )
Parameters
Returns
Returns a pointer to the destination string.
Comments
Use this function instead of the standard strcpy
routine.
This function does not work properly with overlapping strings.
StrDelocalizeNumber Function
Purpose
Delocalize a number passed in as a string. Convert the number from any localized notation to US notation (decimal point and thousandth comma). The current thousand and decimal separators have to be passed in.
Declared In
StringMgr.h
Prototype
Char *StrDelocalizeNumber ( Char *s, Char thousandSeparator, Char decimalSeparator )
Parameters
-
↔
s
- Pointer to the number as an ASCII string.
-
→
thousandSeparator
- Current thousand separator.
-
→
decimalSeparator
- Current decimal separator.
Returns
Returns a pointer to the changed number and modifies the string in s
.
Comments
The current thousandSeparator
and decimalSeparator
can be determined by obtaining the value of the prefNumberFormat
preference using PrefGetPreference()
and then passing the returned NumberFormatType
to LocGetNumberSeparators()
. For example:
Char *localizedNum; NumberFormatType numFormat; Char thousandsSeparator, decimalSeparator; numFormat = (NumberFormatType) PrefGetPreference(prefNumberFormat); LocGetNumberSeparators(numFormat, &thousandsSeparator, &decimalSeparator); StrDelocalizeNumber(localizedNum, thousandsSeparator, decimalSeparator);
Compatibility
Implemented only if 2.0 New Feature Set is present.
See Also
StrLocalizeNumber()
, LocGetNumberSeparators()
StrIToA Function
Purpose
Declared In
StringMgr.h
Prototype
Char *StrIToA ( Char *s, Int32 i )
Parameters
-
←
s
- Pointer to a string of size
maxStrIToALen
in which to store the results. -
→
i
- Integer to convert.
Returns
Returns a pointer to the result string.
See Also
StrIToH Function
Purpose
Convert an integer to hexadecimal ASCII.
Declared In
StringMgr.h
Prototype
Char *StrIToH ( Char *s, UInt32 i )
Parameters
Returns
See Also
StrLen Function
Purpose
Compute the length of a string.
Declared In
StringMgr.h
Prototype
UInt16 StrLen ( const Char *src )
Parameters
Returns
Returns the length of the string in bytes.
Comments
Use this function instead of the standard strlen
routine.
This function returns the length of the string in bytes. On systems that support multi-byte characters, the number returned does not always equal the number of characters.
Compatibility
In Palm OS 3.5 and Palm OS 4.x this function was declared to return an Int16
. In Palm OS Garnet, and prior to Palm OS 3.5, this function returns a UInt16
.
StrLocalizeNumber Function
Purpose
Convert a number (passed in as a string) to localized format, using a specified thousands separator and decimal separator.
Declared In
StringMgr.h
Prototype
Char *StrLocalizeNumber ( Char *s, Char thousandSeparator, Char decimalSeparator )
Parameters
-
↔
s
- Numeric ASCII string to localize.
-
→
thousandSeparator
- Localized thousand separator.
-
→
decimalSeparator
- Localized decimal separator.
Returns
Returns a pointer to the changed number. Converts the number string in s
by replacing all occurrences of "," with thousandSeparator
and all occurrences of "." with decimalSeparator
.
Comments
The current thousandSeparator
and decimalSeparator
can be determined by obtaining the value of the prefNumberFormat
preference using PrefGetPreference()
and then passing the returned NumberFormatType
to LocGetNumberSeparators()
. For example:
Char *localizedNum; NumberFormatType numFormat; Char thousandsSeparator, decimalSeparator; numFormat = (NumberFormatType) PrefGetPreference(prefNumberFormat); LocGetNumberSeparators(numFormat, &thousandsSeparator, &decimalSeparator); StrLocalizeNumber(localizedNum, thousandsSeparator, decimalSeparator);
Compatibility
Implemented only if 2.0 New Feature Set is present.
See Also
StrNCaselessCompare Function
Purpose
Compares two strings out to n characters with case and accent insensitivity.
Declared In
StringMgr.h
Prototype
Int16 StrNCaselessCompare ( const Char *s1, const Char *s2, Int32 n )
Parameters
-
→
s1
- Pointer to the first string.
-
→
s2
- Pointer to the second string.
-
→
n
- Length in bytes of the text to compare.
Returns
Returns 0 if the strings match.
Returns a positive number if s1
> s2
.
Returns a negative number if s1
< s2
.
Comments
To support systems that use multi-byte character encodings, consider using TxtCaselessCompare()
instead of this function (or TxtCompare()
for a case-sensitive comparison). Both functions can match single-byte characters with their multi-byte equivalents, but TxtCaselessCompare
can also return the length of the matching text.
Compatibility
Implemented only if 2.0 New Feature Set is present. As of Palm OS 4.0, both s1
and s2
must be null-terminated strings.
See Also
StrNCompare()
, StrCaselessCompare()
, TxtCaselessCompare()
, StrCompare()
StrNCat Function
Purpose
Concatenates one string to another clipping the destination string to a maximum of n bytes (including the null character at the end).
IMPORTANT: The Palm OS implementation of
StrNCat
differs from the implementation in the standard C library. See the Comments section for details.
Declared In
StringMgr.h
Prototype
Char *StrNCat ( Char *dst, const Char *src, Int16 n )
Parameters
-
→
dst
- Pointer to the null-terminated destination string.
-
→
src
- Pointer to the source string.
-
→
n
- Maximum length in bytes for
dst
, including the terminating null character.
Returns
Returns a pointer to the destination string.
Comments
This function differs from the standard C strncat
function in these ways:
-
StrNCat
treats the parametern
as the maximum length in bytes fordst
. That means it will copy at mostn
-StrLen(dst)
- 1 bytes fromsrc
. The standard C function always copiesn
bytes fromsrc
intodst
. (It copies the entiresrc
intodst
if the length ofsrc
is less thann
). - If the length of the destination string reaches
n
- 1,StrNCat
stops copying bytes fromsrc
and appends the terminating null character todst
. If the length of the destination string is already greater than or equal ton
- 1 before the copying begins,StrNCat
does not copy any data fromsrc
. - In the standard C function, if
src
is less thann
, the entiresrc
string is copied intodst
and then the remaining space is filled with null characters.StrNCat
does not fill the remaining space with null characters in released ROMs. In debug ROMs,StrNCat
fills the remaining bytes with the value0xFE
.
On systems with multi-byte character encodings, this function makes sure that it does not copy part of a multi-byte character. If the last byte copied from src
contains the high-order or middle byte of a multi-byte character, StrNCat
backs up in dst
until the byte after the end of the previous character, and replaces that byte with a null character.
Compatibility
Implemented only if 2.0 New Feature Set is present.
StrNCompare Function
Purpose
Compare two strings out to n bytes. This function is case and accent sensitive.
Declared In
StringMgr.h
Prototype
Int16 StrNCompare ( const Char *s1, const Char *s2, Int32 n )
Parameters
Returns
Returns 0 if the strings match.
Returns a positive number if s1
> s2
.
Returns a negative number if s1
< s2
.
Comments
To support systems that use multi-byte character encodings, consider using TxtCompare()
instead of this function. Both functions can match single-byte characters with their multi-byte equivalents, but TxtCompare
can also return the length of the matching text.
Compatibility
Implemented only if 2.0 New Feature Set is present. As of Palm OS 4.0, both s1
and s2
must be null-terminated strings.
See Also
StrCompare()
, StrNCaselessCompare()
, StrCaselessCompare()
, TxtCaselessCompare()
, StrNCompareAscii()
StrNCompareAscii Function
Purpose
Compare two ASCII strings out to n bytes.
Declared In
StringMgr.h
Prototype
Int16 StrNCompareAscii ( const Char *s1, const Char *s2, Int32 n )
Parameters
Returns
Returns 0 if the strings match.
Returns a positive number if s1
sorts after s2
alphabetically.
Returns a negative number if s1
sorts before s2
alphabetically.
Comments
Use this function instead of the standard strncmp
routine. Use it to do case-sensitive comparisons on strings that are guaranteed to be 7-bit ASCII strings. This function performs a fast, simple byte-to-byte comparison that is guaranteed never to change.
Compatibility
Implemented only if 4.0 New Feature Set is present.
See Also
StrCompare()
, StrNCompare()
, TxtCompare()
, StrCaselessCompare()
, StrNCaselessCompare()
, TxtCaselessCompare()
, StrCompareAscii()
StrNCopy Function
Purpose
Copies up to n bytes from a source string to the destination string. Terminates dst
string at index n-1 if the source string length was n-1 or less.
Declared In
StringMgr.h
Prototype
Char *StrNCopy ( Char *dst, const Char *src, Int16 n )
Parameters
-
→
dst
- Pointer to the destination string.
-
→
src
- Pointer to the source string.
-
→
n
- Maximum number of bytes to copy from
src
string.
Returns
Comments
On systems with multi-byte character encodings, this function makes sure that it does not copy part of a multi-byte character. If the n
th byte of src
contains the high-order or middle byte of a multi-byte character, StrNCopy
backs up in dst
until the byte after the end of the previous character, and replaces the remaining bytes (up to n
-1) with nulls.
Be aware that the n
th byte of dst
upon return may contain the last byte of a multi-byte character. If you plan to terminate the string by setting its last character to NULL, you must not pass the entire length of the string to StrNCopy
. If you do, your code may overwrite the final byte of the last character.
// WRONG! You may overwrite part of multi-byte // character. Char dst[n]; StrNCopy(dst, src, n); dst[n-1] = chrNull;
Instead, if you write to the last byte of the destination string, pass one less than the size of the string to StrNCopy
.
// RIGHT. Instead pass n-1 to StrNCopy. Char dst[n]; StrNCopy(dst, src, n-1); dst[n-1] = chrNull;
Compatibility
Implemented only if 2.0 New Feature Set is present.
StrPrintF Function
Purpose
Implements a subset of the ANSI C sprintf
call, which writes formatted output to a string.
Declared In
StringMgr.h
Prototype
Int16 StrPrintF ( Char *s, const Char *formatStr, ... )
Parameters
-
→
s
- Pointer to a string into which the results are written.
-
→
formatStr
- Pointer to the format specification string.
-
...
- Zero or more arguments to be formatted as specified by
formatStr
.
Returns
Number of characters written to destination string. Returns a negative number if there is an error.
Comments
This function internally calls StrVPrintF()
to do the formatting. See that function's description—specifically, Table 52.1—for details on which format specifications are supported.
Compatibility
Implemented only if 2.0 New Feature Set is present.
See Also
StrStr Function
Purpose
Look for a substring within a string.
Declared In
StringMgr.h
Prototype
Char *StrStr ( const Char *str, const Char *token )
Parameters
Returns
Returns a pointer to the first occurrence of token
in str
or NULL
if not found.
Comments
Use this function instead of the standard strstr
routine.
On systems with multi-byte character encodings, this function makes sure that it does not match only part of a multi-byte character. If the matching strings begins at an inter-character boundary, then this function returns NULL
.
NOTE: If the value of the
token
parameter is the empty string, this function returns NULL
. This is different than the standard strstr
function, which returns str
when token
is the empty string.
See Also
StrToLower Function
Purpose
Convert all the characters in a string to lowercase.
Declared In
StringMgr.h
Prototype
Char *StrToLower ( Char *dst, const Char *src )
Parameters
Returns
Returns a pointer to the destination string.
Compatibility
Prior to Palm OS version 3.5, this function only converted accented characters on Japanese devices. On Palm OS version 3.5 and higher, all characters are appropriately lowercased, including accented characters on Latin devices.
StrVPrintF Function
Purpose
Implements a subset of the ANSI C vsprintf
call, which writes formatted output to a string.
Declared In
StringMgr.h
Prototype
Int16 StrVPrintF ( Char *s, const Char *formatStr, va_list arg )
Parameters
-
←
s
- Pointer to a string into which the results are written. This string is always terminated by a null terminator.
-
→
formatStr
- Pointer to the format specification string.
-
→
arg
- Pointer to a list of zero or more parameters to be formatted as specified by the
formatStr
string.
Returns
Number of characters written to destination string, not including the null terminator. Returns a negative number if there is an error.
Comments
Like the C vsprintf
function, this function is designed to be called by your own function that takes a variable number of arguments and passes them to this function. For details on how to use it, see "Using the StrVPrintF Function" of the Palm OS Programmer's Companion, vol. I, or refer to vsprintf
in a standard C reference book.
Currently, only the conversion specifications %d
, %i
, %u
, %x
, %s
, and %c
are implemented by StrVPrintF
(and related functions). Optional modifiers that are supported include: +
, -
, <space>, *
, <digits>, h
and l
(long). Following is a brief description of how these format specifications work (see a C book for more details).
Each conversion specification begins with the % character. Following the % character, there may be one or more of the characters list in Table 52.1, in sequence.
Example
Here's an example of how to use this call:
#include <unix_stdarg.h> void MyPrintF(Char *s, Char *formatStr, ...) { va_list args; Char text[0x100]; va_start(args, formatStr); StrVPrintF(text, formatStr, args); va_end(args); MyPutS(text); }
Compatibility
Implemented only if 2.0 New Feature Set is present.