utf8-bom: introduce skip_utf8_bom() helper
With the recent change to ignore the UTF8 BOM at the beginning of .gitignore files, we now have two codepaths that do such a skipping (the other one is for reading the configuration files). Introduce utf8_bom[] constant string and skip_utf8_bom() helper and teach .gitignore code how to use it. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Apr 16, 2015 at 10:45 UTC
dde843e7378f65004415bd108038659de9ce2abd
3 files changed
+18
-5
dir.c
+4
-5
@@ -12,6 +12,7 @@
12
#include "refs.h"
13
#include "wildmatch.h"
14
#include "pathspec.h"
15
+#include "utf8.h"
16
17
struct path_simplify {
18
int len;
@@ -538,7 +539,6 @@ int add_excludes_from_file_to_list(const char *fname,
539
struct stat st;
540
int fd, i, lineno = 1;
541
size_t size = 0;
541
- static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
542
char *buf, *entry;
543
544
fd = open(fname, O_RDONLY);
@@ -576,10 +576,9 @@ int add_excludes_from_file_to_list(const char *fname,
576
577
el->filebuf = buf;
578
579
- if (size >= 3 && !memcmp(buf, utf8_bom, 3)) {
580
- buf += 3;
581
- size -= 3;
582
- }
579
+ if (skip_utf8_bom(&buf, size))
580
+ size -= buf - el->filebuf;
581
+
582
entry = buf;
583
584
for (i = 0; i < size; i++) {
utf8.c
+11
@@ -633,3 +633,14 @@ int is_hfs_dotgit(const char *path)
633
634
return 1;
635
}
636
+
637
+const char utf8_bom[] = "\357\273\277";
638
+
639
+int skip_utf8_bom(char **text, size_t len)
640
+{
641
+ if (len < strlen(utf8_bom) ||
642
+ memcmp(*text, utf8_bom, strlen(utf8_bom)))
643
+ return 0;
644
+ *text += strlen(utf8_bom);
645
+ return 1;
646
+}
utf8.h
+3
@@ -13,6 +13,9 @@ int same_encoding(const char *, const char *);
13
__attribute__((format (printf, 2, 3)))
14
int utf8_fprintf(FILE *, const char *, ...);
15
16
+extern const char utf8_bom[];
17
+extern int skip_utf8_bom(char **, size_t);
18
+
19
void strbuf_add_wrapped_text(struct strbuf *buf,
20
const char *text, int indent, int indent2, int width);
21
void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len,