blob: 35df76086a6c91a414190f7af8d05f0f0042ecda [file] [log] [blame]
Johannes Schindelin9e832662006-12-22 22:06:08 +01001#ifndef GIT_UTF8_H
2#define GIT_UTF8_H
3
Elijah Newrenef3ca952018-08-15 10:54:05 -07004struct strbuf;
5
Junio C Hamano396ccf12008-01-06 19:02:22 -08006typedef unsigned int ucs_char_t; /* assuming 32bit int */
7
Nguyễn Thái Ngọc Duy16406322013-04-19 09:08:52 +10008size_t display_mode_esc_sequence_len(const char *s);
Junio C Hamano44b25b82008-01-02 01:49:58 -08009int utf8_width(const char **start, size_t *remainder_p);
Patrick Steinhardt522cc872022-12-01 15:46:53 +010010int utf8_strnwidth(const char *string, size_t len, int skip_ansi);
Geoffrey Thomas8a9391e2009-01-30 04:41:28 -050011int utf8_strwidth(const char *string);
Johannes Schindelin9e832662006-12-22 22:06:08 +010012int is_utf8(const char *text);
Junio C Hamano677cfed2006-12-30 12:20:43 -080013int is_encoding_utf8(const char *name);
Junio C Hamano0e18bcd2012-10-18 22:41:56 -070014int same_encoding(const char *, const char *);
Jeff King46210852013-07-09 20:18:40 -040015__attribute__((format (printf, 2, 3)))
Jiang Xinc0821962013-02-09 14:31:09 +080016int utf8_fprintf(FILE *, const char *, ...);
Junio C Hamano677cfed2006-12-30 12:20:43 -080017
Junio C Hamanodde843e2015-04-16 10:45:29 -070018extern const char utf8_bom[];
Denton Liu55454422019-04-29 04:28:14 -040019int skip_utf8_bom(char **, size_t);
Junio C Hamanodde843e2015-04-16 10:45:29 -070020
Steffen Prohaskae0db1762012-12-11 06:59:22 +010021void strbuf_add_wrapped_text(struct strbuf *buf,
Johannes Schindelina94410c2008-11-10 18:47:00 +010022 const char *text, int indent, int indent2, int width);
Steffen Prohaskae0db1762012-12-11 06:59:22 +010023void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len,
Jeff King98acc832011-02-23 04:50:19 -050024 int indent, int indent2, int width);
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +100025void strbuf_utf8_replace(struct strbuf *sb, int pos, int width,
26 const char *subst);
Johannes Schindelin9e832662006-12-22 22:06:08 +010027
Junio C Hamanob45974a2006-12-23 23:36:55 -080028#ifndef NO_ICONV
Nguyễn Thái Ngọc Duyb782bba2013-04-19 09:08:46 +100029char *reencode_string_iconv(const char *in, size_t insz,
Torsten Bögershausenaab2a1a2019-01-30 16:01:52 +010030 iconv_t conv, size_t bom_len, size_t *outsz);
Jeff Kingc7d017d2018-07-24 06:50:33 -040031char *reencode_string_len(const char *in, size_t insz,
Nguyễn Thái Ngọc Duyb782bba2013-04-19 09:08:46 +100032 const char *out_encoding,
33 const char *in_encoding,
Jeff Kingc7d017d2018-07-24 06:50:33 -040034 size_t *outsz);
Junio C Hamanob45974a2006-12-23 23:36:55 -080035#else
Jeff Kingc7d017d2018-07-24 06:50:33 -040036static inline char *reencode_string_len(const char *a, size_t b,
37 const char *c, const char *d, size_t *e)
Eric Sunshinee654eb22015-06-05 02:42:16 -040038{ if (e) *e = 0; return NULL; }
Junio C Hamanob45974a2006-12-23 23:36:55 -080039#endif
40
Nguyễn Thái Ngọc Duyb782bba2013-04-19 09:08:46 +100041static inline char *reencode_string(const char *in,
42 const char *out_encoding,
43 const char *in_encoding)
44{
45 return reencode_string_len(in, strlen(in),
46 out_encoding, in_encoding,
47 NULL);
48}
49
Kirill Smelkov6cd3c052013-03-07 14:55:07 +040050int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding);
51
Jeff King6162a1d2014-12-15 17:56:59 -050052/*
Li Peng832c0e52016-05-06 20:36:46 +080053 * Returns true if the path would match ".git" after HFS case-folding.
Jeff King6162a1d2014-12-15 17:56:59 -050054 * The path should be NUL-terminated, but we will match variants of both ".git\0"
55 * and ".git/..." (but _not_ ".../.git"). This makes it suitable for both fsck
56 * and verify_path().
Jeff King0fc333b2018-05-02 15:23:45 -040057 *
58 * Likewise, the is_hfs_dotgitfoo() variants look for ".gitfoo".
Jeff King6162a1d2014-12-15 17:56:59 -050059 */
60int is_hfs_dotgit(const char *path);
Jeff King0fc333b2018-05-02 15:23:45 -040061int is_hfs_dotgitmodules(const char *path);
62int is_hfs_dotgitignore(const char *path);
63int is_hfs_dotgitattributes(const char *path);
Jeff King801ed012021-05-03 16:43:22 -040064int is_hfs_dotmailmap(const char *path);
Jeff King6162a1d2014-12-15 17:56:59 -050065
Karthik Nayak110dcda2015-09-10 21:18:19 +053066typedef enum {
67 ALIGN_LEFT,
68 ALIGN_MIDDLE,
69 ALIGN_RIGHT
70} align_type;
71
72/*
73 * Align the string given and store it into a strbuf as per the
74 * 'position' and 'width'. If the given string length is larger than
75 * 'width' than then the input string is not truncated and no
76 * alignment is done.
77 */
78void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
79 const char *s);
80
Lars Schneider10ecb822018-04-15 20:16:05 +020081/*
82 * If a data stream is declared as UTF-16BE or UTF-16LE, then a UTF-16
83 * BOM must not be used [1]. The same applies for the UTF-32 equivalents.
84 * The function returns true if this rule is violated.
85 *
Josh Sorefd05b08c2023-11-24 03:35:13 +000086 * [1] https://unicode.org/faq/utf_bom.html#bom10
Lars Schneider10ecb822018-04-15 20:16:05 +020087 */
88int has_prohibited_utf_bom(const char *enc, const char *data, size_t len);
89
Lars Schneiderc6e48652018-04-15 20:16:06 +020090/*
91 * If the endianness is not defined in the encoding name, then we
92 * require a BOM. The function returns true if a required BOM is missing.
93 *
94 * The Unicode standard instructs to assume big-endian if there in no
95 * BOM for UTF-16/32 [1][2]. However, the W3C/WHATWG encoding standard
96 * used in HTML5 recommends to assume little-endian to "deal with
97 * deployed content" [3].
98 *
99 * Therefore, strictly requiring a BOM seems to be the safest option for
100 * content in Git.
101 *
Josh Sorefd05b08c2023-11-24 03:35:13 +0000102 * [1] https://unicode.org/faq/utf_bom.html#gen6
103 * [2] https://www.unicode.org/versions/Unicode10.0.0/ch03.pdf
Lars Schneiderc6e48652018-04-15 20:16:06 +0200104 * Section 3.10, D98, page 132
105 * [3] https://encoding.spec.whatwg.org/#utf-16le
106 */
107int is_missing_required_utf_bom(const char *enc, const char *data, size_t len);
108
Johannes Schindelin9e832662006-12-22 22:06:08 +0100109#endif