c_src/ustring.c
ERL_NIF_TERM ustring_length(ErlNifEnv* env, int argc,
                            const ERL_NIF_TERM argv[]) {
    ErlNifBinary bin;
    char type[10];
    int len;
    UBreakIterator* iter;
    UErrorCode ec = U_ZERO_ERROR;

    if(!enif_inspect_binary(env, argv[0], &bin))
        return enif_make_badarg(env);
    if(!enif_get_atom_compat(env, argv[1], type, 10, ERL_NIF_LATIN1))
        return enif_make_badarg(env);

    switch(type[0]) {
    case 'c': /* codeunits */
        /* this library assumes that binaries are whole-strings,
           not null-terminated strings */
        return enif_make_int(env, bin.size/2);
    case 'g': /* graphemes */
        iter = ubrk_open(UBRK_CHARACTER, NULL,
                         (UChar*)bin.data, bin.size/2, &ec);

        if(U_FAILURE(ec))
            return error_tuple(env, u_errorName(ec));

        len = 0;
        while(ubrk_next(iter) != UBRK_DONE)
            len++;

        ubrk_close(iter);
        return enif_make_int(env, len);
    default: /* unknown length type */
        return enif_make_badarg(env);
    }
}
/*
 * Find the length of the input string.  Length is computed as the
 * number of 16-bit characters if the "length type" parameter is the
 * atom 'codeunits'.  Length is computed as the number of user-visible
 * characters if the "length type" parameter is the atom 'graphemes'.
 * Inputs:
 *   0: binary(), the string to find the length of
 *   1: atom(), either 'codeunits' or 'graphemes'
 * Outputs (one of):
 *   integer(), the length of the string
 *   {'error', Reason::string()}, something went wrong
 */
Find the length of the input string.  Length is computed as the
number of 16-bit characters if the "length type" parameter is the
atom 'codeunits'.  Length is computed as the number of user-visible
characters if the "length type" parameter is the atom 'graphemes'.
Find the length of the input string . Length is computed as the number of 16 - bit characters if the " length type " parameter is the atom ' codeunits ' . Length is computed as the number of user - visible characters if the " length type " parameter is the atom ' graphemes ' .
ERL_NIF_TERM ustring_length(ErlNifEnv* env, int argc,
                            const ERL_NIF_TERM argv[]) {
    ErlNifBinary bin;
    char type[10];
    int len;
    UBreakIterator* iter;
    UErrorCode ec = U_ZERO_ERROR;
    if(!enif_inspect_binary(env, argv[0], &bin))
        return enif_make_badarg(env);
    if(!enif_get_atom_compat(env, argv[1], type, 10, ERL_NIF_LATIN1))
        return enif_make_badarg(env);
    switch(type[0]) {
    case 'c': 
        return enif_make_int(env, bin.size/2);
    case 'g': 
        iter = ubrk_open(UBRK_CHARACTER, NULL,
                         (UChar*)bin.data, bin.size/2, &ec);
        if(U_FAILURE(ec))
            return error_tuple(env, u_errorName(ec));
        len = 0;
        while(ubrk_next(iter) != UBRK_DONE)
            len++;
        ubrk_close(iter);
        return enif_make_int(env, len);
    default: 
        return enif_make_badarg(env);
    }
}
ERL_NIF_TERM ustring_length ( ErlNifEnv * env , int argc , const ERL_NIF_TERM argv [ ] ) { ErlNifBinary bin ; char type [ 10 ] ; int len ; UBreakIterator * iter ; UErrorCode ec = U_ZERO_ERROR ; if ( ! enif_inspect_binary ( env , argv [ 0 ] , & bin ) ) return enif_make_badarg ( env ) ; if ( ! enif_get_atom_compat ( env , argv [ 1 ] , type , 10 , ERL_NIF_LATIN1 ) ) return enif_make_badarg ( env ) ; switch ( type [ 0 ] ) { case ' ' : return enif_make_int ( env , bin . size / 2 ) ; case ' ' : iter = ubrk_open ( UBRK_CHARACTER , NULL , ( UChar * ) bin . data , bin . size / 2 , & ec ) ; if ( U_FAILURE ( ec ) ) return error_tuple ( env , u_errorName ( ec ) ) ; len = 0 ; while ( ubrk_next ( iter ) != UBRK_DONE ) len ++ ; ubrk_close ( iter ) ; return enif_make_int ( env , len ) ; default : return enif_make_badarg ( env ) ; } }
Find the length of the input string.
Find the length of the input string .
/* codeunits */ /* this library assumes that binaries are whole-strings,
           not null-terminated strings */ /* graphemes */ /* unknown length type */