utf8prev

Accepts a UTF-8 string and returns the address of the previous character.

Library:LibC
Service:Characters and Strings

Syntax

  #include <utf8.h>
   
  utf8_t *utf8prev (
     const utf8_t  *string);
  

Parameters

string

(OUT) Points to the string to start with.

Return Values

Returns the address of the character previous to the current location in the specified string.

Example

The following example traverses a UTF-8 string character-by-character until it reaches the beginning, each time receiving the character as an integer for comparison with another character that is passed in.

  #include <utf8.h>
  #include <string.h>
  
  utf8_t *utf8rchr
  (
     const utf8_t  *string, // contains potentially different-width characters
     utf8_t        *match   // points to a 1- or 2-byte character
  )
  {
     utf8_t     end;
     uint16_t   ch;         // match character if 2-byte
  
     width = utf8width(match);
     if (!width)
        return (utf8_t *) NULL;
     else if (width == 1)   // if match width is 1, treat as ASCII
        return strrchr(string, *match);
  
     ch  = *(uint16_t *) match;
     end = (utf8_t *) string + utf8len(string);
  
     while ((end = utf8prev(end)) >= string)
     {
        width = (utf8width(end);
        switch (width)
        {
           case 0 :       // (means no character/ill-formed UTF-8 string)
           case 1 :      // can’t match single-byte UTF-8 characters
              break;
  
           case 2 :
              if (*(uint16_t *) end == ch)
                 return end;
              break;
        }
     }
  
     return (utf8_t *) NULL;
  }