4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
26 * Grid UTF-8 utility functions.
29 /* Calculate UTF-8 grid cell size. Data is terminated by 0xff. */
31 grid_utf8_size(const struct grid_utf8
*gu
)
35 for (size
= 0; size
< sizeof gu
->data
; size
++) {
36 if (gu
->data
[size
] == 0xff)
42 /* Copy UTF-8 out into a buffer. */
44 grid_utf8_copy(const struct grid_utf8
*gu
, char *buf
, size_t len
)
48 size
= grid_utf8_size(gu
);
50 fatalx("UTF-8 copy overflow");
51 memcpy(buf
, gu
->data
, size
);
55 /* Set UTF-8 grid data from input UTF-8. */
57 grid_utf8_set(struct grid_utf8
*gu
, const struct utf8_data
*utf8data
)
59 if (utf8data
->size
== 0)
60 fatalx("UTF-8 data empty");
61 if (utf8data
->size
> sizeof gu
->data
)
62 fatalx("UTF-8 data too long");
63 memcpy(gu
->data
, utf8data
->data
, utf8data
->size
);
64 if (utf8data
->size
!= sizeof gu
->data
)
65 gu
->data
[utf8data
->size
] = 0xff;
66 gu
->width
= utf8data
->width
;
69 /* Append UTF-8 character onto the cell data (for combined characters). */
71 grid_utf8_append(struct grid_utf8
*gu
, const struct utf8_data
*utf8data
)
75 old_size
= grid_utf8_size(gu
);
76 if (old_size
+ utf8data
->size
> sizeof gu
->data
)
78 memcpy(gu
->data
+ old_size
, utf8data
->data
, utf8data
->size
);
79 if (old_size
+ utf8data
->size
!= sizeof gu
->data
)
80 gu
->data
[old_size
+ utf8data
->size
] = 0xff;
84 /* Compare two UTF-8 cells. */
86 grid_utf8_compare(const struct grid_utf8
*gu1
, const struct grid_utf8
*gu2
)
90 size
= grid_utf8_size(gu1
);
91 if (size
!= grid_utf8_size(gu2
))
93 if (memcmp(gu1
->data
, gu2
->data
, size
) != 0)