Next: , Previous: Autostr deletion, Up: Automatic strings


10.8 Searching in automatic strings

— Function pointer: Astr_istype_f int (*) (int c)

The type for a characteristic function on characters. Such a function is passed a character c, converted to an ‘int’; it should return non-zero iff c has some property. An example would be the standard C function ‘isalpha’.1

— Function: ssize_t astr_find (const Autostr *astr, ssize_t index, const Autostr *target)

Returns the first position in astr at which the contents of target are found. The first position to look at is the next position after index, or zero if index is negative. If target is not found, -1 is returned.

— Function: ssize_t astr_find_s (const Autostr *astr, ssize_t index, const char *chars)

Returns the first position in astr at which the characters referenced by chars are found. The first position to look at is the next position after index, or zero if index is negative. If the target is not found, -1 is returned.

— Function: ssize_t astr_find_c (const Autostr *astr, ssize_t index, char c)

Returns the first position in astr at which the single character c s found. The first position to look at is the next position after index, or zero if index is negative. If c is not found, -1 is returned.

— Function: ssize_t astr_find_match (const Autostr *astr, ssize_t index, const Autostr *set)
— Function: ssize_t astr_find_match_s (const Autostr *astr, ssize_t index, const char *set)

Returns the first position in astr at which a character in set is found. The first position to look at is the next position after index, or zero if index is negative. If no such character is found, -1 is returned.

— Function: ssize_t astr_find_match_f (const Autostr *astr, ssize_t index, Astr_istype_f func)

Returns the first position in astr at which a character for which func returns a non-zero value is found. The first position to look at is the next position after index, or zero if index is negative. If no suc character is found, -1 is returned.

— Function: ssize_t astr_find_nmatch (const Autostr *astr, ssize_t index, const Autostr *set)
— Function: ssize_t astr_find_nmatch_s (const Autostr *astr, ssize_t index, const char *set)
— Function: ssize_t astr_find_nmatch_f (const Autostr *astr, ssize_t index, Astr_istype_f func)

These functions behave exactly as ‘astr_find_match’, ‘astr_find_match_s’ and ‘astr_find_nmatch_s’ respectively, with the exception that the sense of matching is reversed. That is, ‘astr_find_nmatch’ returns the index of the first character in astr after index which is not found in set, for example.

— Function: ssize_t astr_rfind_c (const Autostr *astr, char c)

Returns the last position in astr at which the character c is found, or -1 if c was not found.

An example of using these functions may be helpful here. Note the similarity between the calling idiom for these functions and for standard C library functions such as ‘getchar’.

     /* Call bar() on ASTR at every position where S is found. */
     void
     foo (const Autostr *astr, const char *s)
     {
         ssize_t i = -1;
     
         while ((i = astr_find_s (astr, i, s)) != -1)
             bar (astr, i);
     }

Footnotes

[1] Many implementations of the standard C library provide macros for functions such as ‘isalpha’ and ‘isspace’. However, note that macros are only recognised as such when they look syntactically like function calls, and that the `ctype' facilities must be provided as functions whose address can be taken.