Skip to content

Commit dde843e

Browse files
committed
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 <[email protected]>
1 parent cb0abea commit dde843e

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

dir.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "refs.h"
1313
#include "wildmatch.h"
1414
#include "pathspec.h"
15+
#include "utf8.h"
1516

1617
struct path_simplify {
1718
int len;
@@ -538,7 +539,6 @@ int add_excludes_from_file_to_list(const char *fname,
538539
struct stat st;
539540
int fd, i, lineno = 1;
540541
size_t size = 0;
541-
static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
542542
char *buf, *entry;
543543

544544
fd = open(fname, O_RDONLY);
@@ -576,10 +576,9 @@ int add_excludes_from_file_to_list(const char *fname,
576576

577577
el->filebuf = buf;
578578

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+
583582
entry = buf;
584583

585584
for (i = 0; i < size; i++) {

utf8.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,3 +633,14 @@ int is_hfs_dotgit(const char *path)
633633

634634
return 1;
635635
}
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ int same_encoding(const char *, const char *);
1313
__attribute__((format (printf, 2, 3)))
1414
int utf8_fprintf(FILE *, const char *, ...);
1515

16+
extern const char utf8_bom[];
17+
extern int skip_utf8_bom(char **, size_t);
18+
1619
void strbuf_add_wrapped_text(struct strbuf *buf,
1720
const char *text, int indent, int indent2, int width);
1821
void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len,

0 commit comments

Comments
 (0)