~ubuntu-branches/ubuntu/oneiric/libchewing/oneiric

« back to all changes in this revision

Viewing changes to src/common/chewing-utf8-util.c

  • Committer: Bazaar Package Importer
  • Author(s): Kanru Chen
  • Date: 2006-05-15 16:17:40 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20060515161740-x7jqtejpdwn6jv72
Tags: 0.3.0-1
* New upstream release
* Bump major version to 3
* Change debhelper compatibility to 5
* Update standards-version to 3.7.2.0
* Not install *.la files anymore.
* Install -data files to libchewing3/chewing instead of chewing
  to maintain the compatibility of old -data packages
* Update README.Debian
* Change libchewing3-data architecture to any (Closes: #303000)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * chewing-utf8-util.c
 
3
 *
 
4
 * Copyright (c) 2005, 2006
 
5
 *      libchewing Core Team. See ChangeLog for details.
 
6
 *
 
7
 * See the file "COPYING" for information on usage and redistribution
 
8
 * of this file.
 
9
 */
 
10
 
 
11
#include <stdio.h>
 
12
#include <string.h>
 
13
#include "chewing-utf8-util.h"
 
14
 
 
15
/* Table of UTF-8 length */
 
16
static char utf8len_tab[256] =
 
17
{
 
18
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
 
19
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
 
20
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
 
21
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
 
22
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /*bogus*/
 
23
        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /*bogus*/
 
24
        2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
 
25
        3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1,
 
26
};
 
27
 
 
28
/* Return length of UTF-8 string */
 
29
int ueStrLen( char *str )
 
30
{
 
31
        int length = 0;
 
32
        char *strptr = str;
 
33
 
 
34
        while ( strptr[ 0 ] != '\0' ) {
 
35
                strptr += ueBytesFromChar( strptr[0] );
 
36
                ++length;
 
37
        }
 
38
        return length;
 
39
}
 
40
 
 
41
/* Return bytes of a UTF-8 character */
 
42
int ueBytesFromChar( unsigned char b )
 
43
{
 
44
        return utf8len_tab[ b ];
 
45
}
 
46
 
 
47
/* Return how many bytes was copied */
 
48
int ueStrNCpy( char dest[], const char *src, size_t n, int end )
 
49
{
 
50
        int i = 0, len = 0;
 
51
        char *iter = (char *) src;
 
52
        for ( i = 0; i < n; i++ ) {
 
53
                len += ueBytesFromChar( iter[ len ] );
 
54
        }
 
55
        memcpy( dest, iter, len );
 
56
        if ( end == STRNCPY_CLOSE )
 
57
                dest[ len ] = '\0';
 
58
        return len;
 
59
}
 
60
 
 
61
char *ueStrSeek( char *src, size_t n )
 
62
{
 
63
        int i = 0;
 
64
        char *iter = src;
 
65
        for ( i = 0; i < n; i++ ) {
 
66
                iter += ueBytesFromChar( iter[0] );
 
67
        }
 
68
        return iter;
 
69
}
 
70