Converted to separate package.
[gedcom-parse.git] / utf8 / utf8.c
1 /* Utility functions for UTF-8
2    Copyright (C) 2001, 2002 Peter Verthez
3
4    The UTF8 tools library is free software; you can redistribute it
5    and/or modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The Gedcom parser library is distributed in the hope that it will be
10    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the Gedcom parser library; if not, write to the
16    Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.
18 */
19
20 /* $Id$ */
21 /* $Name$ */
22
23 #include "utf8tools.h"
24
25 int is_utf8_string(const char* str)
26 {
27   int expect_bytes = 0;
28
29   if (!str) return 0;
30   
31   while (*str) {
32     if ((*str & 0x80) == 0) {
33       /* Looks like an ASCII character */
34       if (expect_bytes)
35         /* byte of UTF-8 character expected */
36         return 0;
37       else {
38         /* OK, ASCII character expected */
39         str++;
40       }
41     }
42     else {
43       /* Looks like byte of an UTF-8 character */
44       if (expect_bytes) {
45         /* expect_bytes already set: first byte of UTF-8 char already seen */
46         if ((*str & 0xC0) == 0x80) {
47           /* OK, next byte of UTF-8 character */
48           /* Decrement number of expected bytes */
49           expect_bytes--;
50           str++;
51         }
52         else {
53           /* again first byte ?!?! */
54           return 0;
55         }
56       }
57       else {
58         /* First byte of the UTF-8 character */
59         /* count initial one bits and set expect_bytes to 1 less */
60         char ch = *str;
61         while (ch & 0x80) {
62           expect_bytes++;
63           ch = (ch & 0x7f) << 1;
64         }
65         expect_bytes--;
66         str++;
67       }
68     }
69   }
70
71   return (expect_bytes == 0);
72 }
73
74 int utf8_strlen(const char* str)
75 {
76   int num_char = 0;
77   
78   if (!str) return 0;
79   
80   while (*str) {
81     if ((*str & 0x80) == 0 || (*str & 0xC0) == 0xC0)
82       num_char++;
83     str++;
84   }
85   
86   return num_char;
87 }
88