Commit e7da560af4553dd989e3860aacf2578d6569296e

Vicent Martí 2012-11-29T00:00:32

Merge pull request #1088 from arrbee/consolidate-text-functions Consolidate text buffer functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
diff --git a/src/buf_text.c b/src/buf_text.c
new file mode 100644
index 0000000..3c5024e
--- /dev/null
+++ b/src/buf_text.c
@@ -0,0 +1,208 @@
+/*
+ * Copyright (C) 2009-2012 the libgit2 contributors
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#include "buf_text.h"
+
+int git_buf_text_puts_escaped(
+	git_buf *buf,
+	const char *string,
+	const char *esc_chars,
+	const char *esc_with)
+{
+	const char *scan;
+	size_t total = 0, esc_len = strlen(esc_with), count;
+
+	if (!string)
+		return 0;
+
+	for (scan = string; *scan; ) {
+		/* count run of non-escaped characters */
+		count = strcspn(scan, esc_chars);
+		total += count;
+		scan += count;
+		/* count run of escaped characters */
+		count = strspn(scan, esc_chars);
+		total += count * (esc_len + 1);
+		scan += count;
+	}
+
+	if (git_buf_grow(buf, buf->size + total + 1) < 0)
+		return -1;
+
+	for (scan = string; *scan; ) {
+		count = strcspn(scan, esc_chars);
+
+		memmove(buf->ptr + buf->size, scan, count);
+		scan += count;
+		buf->size += count;
+
+		for (count = strspn(scan, esc_chars); count > 0; --count) {
+			/* copy escape sequence */
+			memmove(buf->ptr + buf->size, esc_with, esc_len);
+			buf->size += esc_len;
+			/* copy character to be escaped */
+			buf->ptr[buf->size] = *scan;
+			buf->size++;
+			scan++;
+		}
+	}
+
+	buf->ptr[buf->size] = '\0';
+
+	return 0;
+}
+
+void git_buf_text_unescape(git_buf *buf)
+{
+	buf->size = git__unescape(buf->ptr);
+}
+
+int git_buf_text_common_prefix(git_buf *buf, const git_strarray *strings)
+{
+	size_t i;
+	const char *str, *pfx;
+
+	git_buf_clear(buf);
+
+	if (!strings || !strings->count)
+		return 0;
+
+	/* initialize common prefix to first string */
+	if (git_buf_sets(buf, strings->strings[0]) < 0)
+		return -1;
+
+	/* go through the rest of the strings, truncating to shared prefix */
+	for (i = 1; i < strings->count; ++i) {
+
+		for (str = strings->strings[i], pfx = buf->ptr;
+			 *str && *str == *pfx; str++, pfx++)
+			/* scanning */;
+
+		git_buf_truncate(buf, pfx - buf->ptr);
+
+		if (!buf->size)
+			break;
+	}
+
+	return 0;
+}
+
+bool git_buf_text_is_binary(const git_buf *buf)
+{
+	const char *scan = buf->ptr, *end = buf->ptr + buf->size;
+	int printable = 0, nonprintable = 0;
+
+	while (scan < end) {
+		unsigned char c = *scan++;
+
+		if (c > 0x1F && c < 0x7F)
+			printable++;
+		else if (c == '\0')
+			return true;
+		else if (!git__isspace(c))
+			nonprintable++;
+	}
+
+	return ((printable >> 7) < nonprintable);
+}
+
+int git_buf_text_detect_bom(git_bom_t *bom, const git_buf *buf, size_t offset)
+{
+	const char *ptr;
+	size_t len;
+
+	/* need at least 2 bytes after offset to look for any BOM */
+	if (buf->size < offset + 2)
+		return 0;
+
+	ptr = buf->ptr + offset;
+	len = buf->size - offset;
+
+	switch (*ptr++) {
+	case 0:
+		if (len >= 4 && ptr[0] == 0 && ptr[1] == '\xFE' && ptr[2] == '\xFF') {
+			*bom = GIT_BOM_UTF32_BE;
+			return 4;
+		}
+		break;
+	case '\xEF':
+		if (len >= 3 && ptr[0] == '\xBB' && ptr[1] == '\xBF') {
+			*bom = GIT_BOM_UTF8;
+			return 3;
+		}
+		break;
+	case '\xFE':
+		if (*ptr == '\xFF') {
+			*bom = GIT_BOM_UTF16_BE;
+			return 2;
+		}
+		break;
+	case '\xFF':
+		if (*ptr != '\xFE')
+			break;
+		if (len >= 4 && ptr[1] == 0 && ptr[2] == 0) {
+			*bom = GIT_BOM_UTF32_LE;
+			return 4;
+		} else {
+			*bom = GIT_BOM_UTF16_LE;
+			return 2;
+		}
+		break;
+	default:
+		break;
+	}
+
+	return 0;
+}
+
+bool git_buf_text_gather_stats(
+	git_buf_text_stats *stats, const git_buf *buf, bool skip_bom)
+{
+	const char *scan = buf->ptr, *end = buf->ptr + buf->size;
+	int skip;
+
+	memset(stats, 0, sizeof(*stats));
+
+	/* BOM detection */
+	skip = git_buf_text_detect_bom(&stats->bom, buf, 0);
+	if (skip_bom)
+		scan += skip;
+
+	/* Ignore EOF character */
+	if (buf->size > 0 && end[-1] == '\032')
+		end--;
+
+	/* Counting loop */
+	while (scan < end) {
+		unsigned char c = *scan++;
+
+		if ((c > 0x1F && c < 0x7F) || c > 0x9f)
+			stats->printable++;
+		else switch (c) {
+			case '\0':
+				stats->nul++;
+				stats->nonprintable++;
+				break;
+			case '\n':
+				stats->lf++;
+				break;
+			case '\r':
+				stats->cr++;
+				if (scan < end && *scan == '\n')
+					stats->crlf++;
+				break;
+			case '\t': case '\f': case '\v': case '\b': case 0x1b: /*ESC*/
+				stats->printable++;
+				break;
+			default:
+				stats->nonprintable++;
+				break;
+			}
+	}
+
+	return (stats->nul > 0 ||
+		((stats->printable >> 7) < stats->nonprintable));
+}
diff --git a/src/buf_text.h b/src/buf_text.h
new file mode 100644
index 0000000..c22499b
--- /dev/null
+++ b/src/buf_text.h
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2009-2012 the libgit2 contributors
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_buf_text_h__
+#define INCLUDE_buf_text_h__
+
+#include "buffer.h"
+
+typedef enum {
+	GIT_BOM_NONE = 0,
+	GIT_BOM_UTF8 = 1,
+	GIT_BOM_UTF16_LE = 2,
+	GIT_BOM_UTF16_BE = 3,
+	GIT_BOM_UTF32_LE = 4,
+	GIT_BOM_UTF32_BE = 5
+} git_bom_t;
+
+typedef struct {
+	git_bom_t bom; /* BOM found at head of text */
+	unsigned int nul, cr, lf, crlf; /* NUL, CR, LF and CRLF counts */
+	unsigned int printable, nonprintable; /* These are just approximations! */
+} git_buf_text_stats;
+
+/**
+ * Append string to buffer, prefixing each character from `esc_chars` with
+ * `esc_with` string.
+ *
+ * @param buf Buffer to append data to
+ * @param string String to escape and append
+ * @param esc_chars Characters to be escaped
+ * @param esc_with String to insert in from of each found character
+ * @return 0 on success, <0 on failure (probably allocation problem)
+ */
+extern int git_buf_text_puts_escaped(
+	git_buf *buf,
+	const char *string,
+	const char *esc_chars,
+	const char *esc_with);
+
+/**
+ * Append string escaping characters that are regex special
+ */
+GIT_INLINE(int) git_buf_text_puts_escape_regex(git_buf *buf, const char *string)
+{
+	return git_buf_text_puts_escaped(buf, string, "^.[]$()|*+?{}\\", "\\");
+}
+
+/**
+ * Unescape all characters in a buffer in place
+ *
+ * I.e. remove backslashes
+ */
+extern void git_buf_text_unescape(git_buf *buf);
+
+/**
+ * Fill buffer with the common prefix of a array of strings
+ *
+ * Buffer will be set to empty if there is no common prefix
+ */
+extern int git_buf_text_common_prefix(git_buf *buf, const git_strarray *strs);
+
+/**
+ * Check quickly if buffer looks like it contains binary data
+ *
+ * @param buf Buffer to check
+ * @return true if buffer looks like non-text data
+ */
+extern bool git_buf_text_is_binary(const git_buf *buf);
+
+/**
+ * Check if a buffer begins with a UTF BOM
+ *
+ * @param bom Set to the type of BOM detected or GIT_BOM_NONE
+ * @param buf Buffer in which to check the first bytes for a BOM
+ * @param offset Offset into buffer to look for BOM
+ * @return Number of bytes of BOM data (or 0 if no BOM found)
+ */
+extern int git_buf_text_detect_bom(
+	git_bom_t *bom, const git_buf *buf, size_t offset);
+
+/**
+ * Gather stats for a piece of text
+ *
+ * Fill the `stats` structure with counts of unreadable characters, carriage
+ * returns, etc, so it can be used in heuristics.  This automatically skips
+ * a trailing EOF (\032 character).  Also it will look for a BOM at the
+ * start of the text and can be told to skip that as well.
+ *
+ * @param stats Structure to be filled in
+ * @param buf Text to process
+ * @param skip_bom Exclude leading BOM from stats if true
+ * @return Does the buffer heuristically look like binary data
+ */
+extern bool git_buf_text_gather_stats(
+	git_buf_text_stats *stats, const git_buf *buf, bool skip_bom);
+
+#endif
diff --git a/src/buffer.c b/src/buffer.c
index e55b0a2..0e2c005 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -31,15 +31,7 @@ void git_buf_init(git_buf *buf, size_t initial_size)
 		git_buf_grow(buf, initial_size);
 }
 
-int git_buf_grow(git_buf *buf, size_t target_size)
-{
-	int error = git_buf_try_grow(buf, target_size);
-	if (error != 0)
-		buf->ptr = git_buf__oom;
-	return error;
-}
-
-int git_buf_try_grow(git_buf *buf, size_t target_size)
+int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom)
 {
 	char *new_ptr;
 	size_t new_size;
@@ -67,8 +59,12 @@ int git_buf_try_grow(git_buf *buf, size_t target_size)
 	new_size = (new_size + 7) & ~7;
 
 	new_ptr = git__realloc(new_ptr, new_size);
-	if (!new_ptr)
+
+	if (!new_ptr) {
+		if (mark_oom)
+			buf->ptr = git_buf__oom;
 		return -1;
+	}
 
 	buf->asize = new_size;
 	buf->ptr   = new_ptr;
@@ -141,51 +137,6 @@ int git_buf_puts(git_buf *buf, const char *string)
 	return git_buf_put(buf, string, strlen(string));
 }
 
-int git_buf_puts_escaped(
-	git_buf *buf, const char *string, const char *esc_chars, const char *esc_with)
-{
-	const char *scan;
-	size_t total = 0, esc_len = strlen(esc_with), count;
-
-	if (!string)
-		return 0;
-
-	for (scan = string; *scan; ) {
-		/* count run of non-escaped characters */
-		count = strcspn(scan, esc_chars);
-		total += count;
-		scan += count;
-		/* count run of escaped characters */
-		count = strspn(scan, esc_chars);
-		total += count * (esc_len + 1);
-		scan += count;
-	}
-
-	ENSURE_SIZE(buf, buf->size + total + 1);
-
-	for (scan = string; *scan; ) {
-		count = strcspn(scan, esc_chars);
-
-		memmove(buf->ptr + buf->size, scan, count);
-		scan += count;
-		buf->size += count;
-
-		for (count = strspn(scan, esc_chars); count > 0; --count) {
-			/* copy escape sequence */
-			memmove(buf->ptr + buf->size, esc_with, esc_len);
-			buf->size += esc_len;
-			/* copy character to be escaped */
-			buf->ptr[buf->size] = *scan;
-			buf->size++;
-			scan++;
-		}
-	}
-
-	buf->ptr[buf->size] = '\0';
-
-	return 0;
-}
-
 static const char b64str[64] =
 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
@@ -497,59 +448,6 @@ int git_buf_cmp(const git_buf *a, const git_buf *b)
 		(a->size < b->size) ? -1 : (a->size > b->size) ? 1 : 0;
 }
 
-int git_buf_common_prefix(git_buf *buf, const git_strarray *strings)
-{
-	size_t i;
-	const char *str, *pfx;
-
-	git_buf_clear(buf);
-
-	if (!strings || !strings->count)
-		return 0;
-
-	/* initialize common prefix to first string */
-	if (git_buf_sets(buf, strings->strings[0]) < 0)
-		return -1;
-
-	/* go through the rest of the strings, truncating to shared prefix */
-	for (i = 1; i < strings->count; ++i) {
-
-		for (str = strings->strings[i], pfx = buf->ptr;
-			 *str && *str == *pfx; str++, pfx++)
-			/* scanning */;
-
-		git_buf_truncate(buf, pfx - buf->ptr);
-
-		if (!buf->size)
-			break;
-	}
-
-	return 0;
-}
-
-bool git_buf_is_binary(const git_buf *buf)
-{
-	size_t i;
-	int printable = 0, nonprintable = 0;
-
-	for (i = 0; i < buf->size; i++) {
-		unsigned char c = buf->ptr[i];
-		if (c > 0x1F && c < 0x7F)
-			printable++;
-		else if (c == '\0')
-			return true;
-		else if (!git__isspace(c))
-			nonprintable++;
-	}
-
-	return ((printable >> 7) < nonprintable);
-}
-
-void git_buf_unescape(git_buf *buf)
-{
-	buf->size = git__unescape(buf->ptr);
-}
-
 int git_buf_splice(
 	git_buf *buf,
 	size_t where,
diff --git a/src/buffer.h b/src/buffer.h
index a2896d4..379216b 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -27,30 +27,35 @@ extern char git_buf__oom[];
  * For the cases where GIT_BUF_INIT cannot be used to do static
  * initialization.
  */
-void git_buf_init(git_buf *buf, size_t initial_size);
+extern void git_buf_init(git_buf *buf, size_t initial_size);
 
 /**
- * Grow the buffer to hold at least `target_size` bytes.
+ * Attempt to grow the buffer to hold at least `target_size` bytes.
  *
- * If the allocation fails, this will return an error and the buffer
- * will be marked as invalid for future operations.  The existing
- * contents of the buffer will be preserved however.
- * @return 0 on success or -1 on failure
+ * If the allocation fails, this will return an error.  If mark_oom is true,
+ * this will mark the buffer as invalid for future operations; if false,
+ * existing buffer content will be preserved, but calling code must handle
+ * that buffer was not expanded.
  */
-int git_buf_grow(git_buf *buf, size_t target_size);
+extern int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom);
 
 /**
- * Attempt to grow the buffer to hold at least `target_size` bytes.
+ * Grow the buffer to hold at least `target_size` bytes.
  *
- * This is just like `git_buf_grow` except that even if the allocation
- * fails, the git_buf will still be left in a valid state.
+ * If the allocation fails, this will return an error and the buffer will be
+ * marked as invalid for future operations, invaliding contents.
+ *
+ * @return 0 on success or -1 on failure
  */
-int git_buf_try_grow(git_buf *buf, size_t target_size);
+GIT_INLINE(int) git_buf_grow(git_buf *buf, size_t target_size)
+{
+	return git_buf_try_grow(buf, target_size, true);
+}
 
-void git_buf_free(git_buf *buf);
-void git_buf_swap(git_buf *buf_a, git_buf *buf_b);
-char *git_buf_detach(git_buf *buf);
-void git_buf_attach(git_buf *buf, char *ptr, size_t asize);
+extern void git_buf_free(git_buf *buf);
+extern void git_buf_swap(git_buf *buf_a, git_buf *buf_b);
+extern char *git_buf_detach(git_buf *buf);
+extern void git_buf_attach(git_buf *buf, char *ptr, size_t asize);
 
 /**
  * Test if there have been any reallocation failures with this git_buf.
@@ -92,18 +97,6 @@ int git_buf_join_n(git_buf *buf, char separator, int nbuf, ...);
 int git_buf_join(git_buf *buf, char separator, const char *str_a, const char *str_b);
 
 /**
- * Copy string into buf prefixing every character that is contained in the
- * esc_chars string with the esc_with string.
- */
-int git_buf_puts_escaped(
-	git_buf *buf, const char *string, const char *esc_chars, const char *esc_with);
-
-GIT_INLINE(int) git_buf_puts_escape_regex(git_buf *buf, const char *string)
-{
-	return git_buf_puts_escaped(buf, string, "^.[]$()|*+?{}\\", "\\");
-}
-
-/**
  * Join two strings as paths, inserting a slash between as needed.
  * @return 0 on success, -1 on failure
  */
@@ -146,15 +139,6 @@ void git_buf_rtrim(git_buf *buf);
 
 int git_buf_cmp(const git_buf *a, const git_buf *b);
 
-/* Fill buf with the common prefix of a array of strings */
-int git_buf_common_prefix(git_buf *buf, const git_strarray *strings);
-
-/* Check if buffer looks like it contains binary data */
-bool git_buf_is_binary(const git_buf *buf);
-
-/* Unescape all characters in a buffer */
-void git_buf_unescape(git_buf *buf);
-
 /* Write data as base64 encoded in buffer */
 int git_buf_put_base64(git_buf *buf, const char *data, size_t len);
 
diff --git a/src/config.c b/src/config.c
index 5ee0d39..6347f7d 100644
--- a/src/config.c
+++ b/src/config.c
@@ -10,6 +10,7 @@
 #include "config.h"
 #include "git2/config.h"
 #include "vector.h"
+#include "buf_text.h"
 #if GIT_WIN32
 # include <windows.h>
 #endif
@@ -803,7 +804,7 @@ int git_config_rename_section(
 	int error = -1;
 	struct rename_data data;
 
-	git_buf_puts_escape_regex(&pattern,  old_section_name);
+	git_buf_text_puts_escape_regex(&pattern,  old_section_name);
 	git_buf_puts(&pattern, "\\..+");
 	if (git_buf_oom(&pattern))
 		goto cleanup;
diff --git a/src/config_file.c b/src/config_file.c
index 7cc812a..354a919 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -10,6 +10,7 @@
 #include "fileops.h"
 #include "filebuf.h"
 #include "buffer.h"
+#include "buf_text.h"
 #include "git2/config.h"
 #include "git2/types.h"
 #include "strmap.h"
@@ -854,17 +855,14 @@ fail_parse:
 
 static int skip_bom(diskfile_backend *cfg)
 {
-	static const char utf8_bom[] = { '\xef', '\xbb', '\xbf' };
+	git_bom_t bom;
+	int bom_offset = git_buf_text_detect_bom(&bom,
+		&cfg->reader.buffer, cfg->reader.read_ptr - cfg->reader.buffer.ptr);
 
-	if (cfg->reader.buffer.size < sizeof(utf8_bom))
-		return 0;
-
-	if (memcmp(cfg->reader.read_ptr, utf8_bom, sizeof(utf8_bom)) == 0)
-		cfg->reader.read_ptr += sizeof(utf8_bom);
+	if (bom == GIT_BOM_UTF8)
+		cfg->reader.read_ptr += bom_offset;
 
-	/* TODO: the reference implementation does pretty stupid
-		stuff with the BoM
-	*/
+	/* TODO: reference implementation is pretty stupid with BoM */
 
 	return 0;
 }
diff --git a/src/crlf.c b/src/crlf.c
index 5e86b4e..80204eb 100644
--- a/src/crlf.c
+++ b/src/crlf.c
@@ -148,8 +148,11 @@ static int crlf_apply_to_odb(git_filter *self, git_buf *dest, const git_buf *sou
 	if (filter->attrs.crlf_action == GIT_CRLF_AUTO ||
 		filter->attrs.crlf_action == GIT_CRLF_GUESS) {
 
-		git_text_stats stats;
-		git_text_gather_stats(&stats, source);
+		git_buf_text_stats stats;
+
+		/* Check heuristics for binary vs text... */
+		if (git_buf_text_gather_stats(&stats, source, false))
+			return -1;
 
 		/*
 		 * We're currently not going to even try to convert stuff
@@ -159,12 +162,6 @@ static int crlf_apply_to_odb(git_filter *self, git_buf *dest, const git_buf *sou
 		if (stats.cr != stats.crlf)
 			return -1;
 
-		/*
-		 * And add some heuristics for binary vs text, of course...
-		 */
-		if (git_text_is_binary(&stats))
-			return -1;
-
 #if 0
 		if (crlf_action == CRLF_GUESS) {
 			/*
diff --git a/src/diff_output.c b/src/diff_output.c
index 3d5e03a..e137fd0 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -142,7 +142,7 @@ static int diff_delta_is_binary_by_content(
 		search.ptr  = map->data;
 		search.size = min(map->len, 4000);
 
-		if (git_buf_is_binary(&search))
+		if (git_buf_text_is_binary(&search))
 			file->flags |= GIT_DIFF_FILE_BINARY;
 		else
 			file->flags |= GIT_DIFF_FILE_NOT_BINARY;
diff --git a/src/filter.c b/src/filter.c
index f2ab1b8..6d27c0c 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -13,75 +13,6 @@
 #include "git2/config.h"
 #include "blob.h"
 
-/* Tweaked from Core Git. I wonder what we could use this for... */
-void git_text_gather_stats(git_text_stats *stats, const git_buf *text)
-{
-	size_t i;
-
-	memset(stats, 0, sizeof(*stats));
-
-	for (i = 0; i < git_buf_len(text); i++) {
-		unsigned char c = text->ptr[i];
-
-		if (c == '\r') {
-			stats->cr++;
-
-			if (i + 1 < git_buf_len(text) && text->ptr[i + 1] == '\n')
-				stats->crlf++;
-		}
-
-		else if (c == '\n')
-			stats->lf++;
-
-		else if (c == 127)
-			/* DEL */
-			stats->nonprintable++;
-
-		else if (c <= 0x1F || (c >= 0x80 && c <= 0x9F)) {
-			switch (c) {
-				/* BS, HT, ESC and FF */
-			case '\b': case '\t': case '\033': case '\014':
-				stats->printable++;
-				break;
-			case 0:
-				stats->nul++;
-				/* fall through */
-			default:
-				stats->nonprintable++;
-			}
-		}
-
-		else
-			stats->printable++;
-	}
-
-	/* If file ends with EOF then don't count this EOF as non-printable. */
-	if (git_buf_len(text) >= 1 && text->ptr[text->size - 1] == '\032')
-		stats->nonprintable--;
-}
-
-/*
- * Fresh from Core Git
- */
-int git_text_is_binary(git_text_stats *stats)
-{
-	if (stats->nul)
-		return 1;
-
-	if ((stats->printable >> 7) < stats->nonprintable)
-		return 1;
-	/*
-	 * Other heuristics? Average line length might be relevant,
-	 * as might LF vs CR vs CRLF counts..
-	 *
-	 * NOTE! It might be normal to have a low ratio of CRLF to LF
-	 * (somebody starts with a LF-only file and edits it with an editor
-	 * that adds CRLF only to lines that are added..). But do  we
-	 * want to support CR-only? Probably not.
-	 */
-	return 0;
-}
-
 int git_filters_load(git_vector *filters, git_repository *repo, const char *path, int mode)
 {
 	int error;
diff --git a/src/filter.h b/src/filter.h
index b9beb49..2b5051c 100644
--- a/src/filter.h
+++ b/src/filter.h
@@ -9,6 +9,7 @@
 
 #include "common.h"
 #include "buffer.h"
+#include "buf_text.h"
 #include "git2/odb.h"
 #include "git2/repository.h"
 
@@ -31,14 +32,6 @@ typedef enum {
 	GIT_CRLF_AUTO,
 } git_crlf_t;
 
-typedef struct {
-	/* NUL, CR, LF and CRLF counts */
-	unsigned int nul, cr, lf, crlf;
-
-	/* These are just approximations! */
-	unsigned int printable, nonprintable;
-} git_text_stats;
-
 /*
  * FILTER API
  */
@@ -99,24 +92,4 @@ extern int git_filter_add__crlf_to_odb(git_vector *filters, git_repository *repo
 /* Add CRLF, from ODB to worktree */
 extern int git_filter_add__crlf_to_workdir(git_vector *filters, git_repository *repo, const char *path);
 
-
-/*
- * PLAINTEXT API
- */
-
-/*
- * Gather stats for a piece of text
- *
- * Fill the `stats` structure with information on the number of
- * unreadable characters, carriage returns, etc, so it can be
- * used in heuristics.
- */
-extern void git_text_gather_stats(git_text_stats *stats, const git_buf *text);
-
-/*
- * Process `git_text_stats` data generated by `git_text_stat` to see
- * if it qualifies as a binary file
- */
-extern int git_text_is_binary(git_text_stats *stats);
-
 #endif
diff --git a/src/path.c b/src/path.c
index 98351be..87eded3 100644
--- a/src/path.c
+++ b/src/path.c
@@ -511,7 +511,7 @@ static bool _check_dir_contents(
 	size_t sub_size = strlen(sub);
 
 	/* leave base valid even if we could not make space for subdir */
-	if (git_buf_try_grow(dir, dir_size + sub_size + 2) < 0)
+	if (git_buf_try_grow(dir, dir_size + sub_size + 2, false) < 0)
 		return false;
 
 	/* save excursion */
diff --git a/src/pathspec.c b/src/pathspec.c
index fc6547a..993b446 100644
--- a/src/pathspec.c
+++ b/src/pathspec.c
@@ -6,6 +6,7 @@
  */
 
 #include "pathspec.h"
+#include "buf_text.h"
 #include "attr_file.h"
 
 /* what is the common non-wildcard prefix for all items in the pathspec */
@@ -15,7 +16,7 @@ char *git_pathspec_prefix(const git_strarray *pathspec)
 	const char *scan;
 
 	if (!pathspec || !pathspec->count ||
-		git_buf_common_prefix(&prefix, pathspec) < 0)
+		git_buf_text_common_prefix(&prefix, pathspec) < 0)
 		return NULL;
 
 	/* diff prefix will only be leading non-wildcards */
@@ -31,7 +32,7 @@ char *git_pathspec_prefix(const git_strarray *pathspec)
 		return NULL;
 	}
 
-	git_buf_unescape(&prefix);
+	git_buf_text_unescape(&prefix);
 
 	return git_buf_detach(&prefix);
 }
diff --git a/src/submodule.c b/src/submodule.c
index 15158f0..c117255 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -12,6 +12,7 @@
 #include "git2/index.h"
 #include "git2/submodule.h"
 #include "buffer.h"
+#include "buf_text.h"
 #include "vector.h"
 #include "posix.h"
 #include "config_file.h"
@@ -782,7 +783,7 @@ int git_submodule_reload(git_submodule *submodule)
 		git_buf path = GIT_BUF_INIT;
 
 		git_buf_sets(&path, "submodule\\.");
-		git_buf_puts_escape_regex(&path, submodule->name);
+		git_buf_text_puts_escape_regex(&path, submodule->name);
 		git_buf_puts(&path, ".*");
 
 		if (git_buf_oom(&path))
diff --git a/tests-clar/core/buffer.c b/tests-clar/core/buffer.c
index 236bf39..40fc4c5 100644
--- a/tests-clar/core/buffer.c
+++ b/tests-clar/core/buffer.c
@@ -1,5 +1,6 @@
 #include "clar_libgit2.h"
 #include "buffer.h"
+#include "buf_text.h"
 
 #define TESTSTR "Have you seen that? Have you seeeen that??"
 const char *test_string = TESTSTR;
@@ -576,37 +577,37 @@ void test_core_buffer__11(void)
 
 	t.strings = t1;
 	t.count = 3;
-	cl_git_pass(git_buf_common_prefix(&a, &t));
+	cl_git_pass(git_buf_text_common_prefix(&a, &t));
 	cl_assert_equal_s(a.ptr, "");
 
 	t.strings = t2;
 	t.count = 3;
-	cl_git_pass(git_buf_common_prefix(&a, &t));
+	cl_git_pass(git_buf_text_common_prefix(&a, &t));
 	cl_assert_equal_s(a.ptr, "some");
 
 	t.strings = t3;
 	t.count = 3;
-	cl_git_pass(git_buf_common_prefix(&a, &t));
+	cl_git_pass(git_buf_text_common_prefix(&a, &t));
 	cl_assert_equal_s(a.ptr, "");
 
 	t.strings = t4;
 	t.count = 3;
-	cl_git_pass(git_buf_common_prefix(&a, &t));
+	cl_git_pass(git_buf_text_common_prefix(&a, &t));
 	cl_assert_equal_s(a.ptr, "happ");
 
 	t.strings = t5;
 	t.count = 3;
-	cl_git_pass(git_buf_common_prefix(&a, &t));
+	cl_git_pass(git_buf_text_common_prefix(&a, &t));
 	cl_assert_equal_s(a.ptr, "happ");
 
 	t.strings = t6;
 	t.count = 3;
-	cl_git_pass(git_buf_common_prefix(&a, &t));
+	cl_git_pass(git_buf_text_common_prefix(&a, &t));
 	cl_assert_equal_s(a.ptr, "");
 
 	t.strings = t7;
 	t.count = 3;
-	cl_git_pass(git_buf_common_prefix(&a, &t));
+	cl_git_pass(git_buf_text_common_prefix(&a, &t));
 	cl_assert_equal_s(a.ptr, "");
 
 	git_buf_free(&a);
@@ -641,19 +642,19 @@ void test_core_buffer__puts_escaped(void)
 	git_buf a = GIT_BUF_INIT;
 
 	git_buf_clear(&a);
-	cl_git_pass(git_buf_puts_escaped(&a, "this is a test", "", ""));
+	cl_git_pass(git_buf_text_puts_escaped(&a, "this is a test", "", ""));
 	cl_assert_equal_s("this is a test", a.ptr);
 
 	git_buf_clear(&a);
-	cl_git_pass(git_buf_puts_escaped(&a, "this is a test", "t", "\\"));
+	cl_git_pass(git_buf_text_puts_escaped(&a, "this is a test", "t", "\\"));
 	cl_assert_equal_s("\\this is a \\tes\\t", a.ptr);
 
 	git_buf_clear(&a);
-	cl_git_pass(git_buf_puts_escaped(&a, "this is a test", "i ", "__"));
+	cl_git_pass(git_buf_text_puts_escaped(&a, "this is a test", "i ", "__"));
 	cl_assert_equal_s("th__is__ __is__ a__ test", a.ptr);
 
 	git_buf_clear(&a);
-	cl_git_pass(git_buf_puts_escape_regex(&a, "^match\\s*[A-Z]+.*"));
+	cl_git_pass(git_buf_text_puts_escape_regex(&a, "^match\\s*[A-Z]+.*"));
 	cl_assert_equal_s("\\^match\\\\s\\*\\[A-Z\\]\\+\\.\\*", a.ptr);
 
 	git_buf_free(&a);
@@ -663,7 +664,7 @@ static void assert_unescape(char *expected, char *to_unescape) {
 	git_buf buf = GIT_BUF_INIT;
 
 	cl_git_pass(git_buf_sets(&buf, to_unescape));
-	git_buf_unescape(&buf);
+	git_buf_text_unescape(&buf);
 	cl_assert_equal_s(expected, buf.ptr);
 	cl_assert_equal_sz(strlen(expected), buf.size);
 
diff --git a/tests-clar/object/blob/filter.c b/tests-clar/object/blob/filter.c
index 7854898..b9bbfff 100644
--- a/tests-clar/object/blob/filter.c
+++ b/tests-clar/object/blob/filter.c
@@ -4,7 +4,7 @@
 #include "filter.h"
 
 static git_repository *g_repo = NULL;
-#define NUM_TEST_OBJECTS 6
+#define NUM_TEST_OBJECTS 8
 static git_oid g_oids[NUM_TEST_OBJECTS];
 static const char *g_raw[NUM_TEST_OBJECTS] = {
 	"",
@@ -12,16 +12,20 @@ static const char *g_raw[NUM_TEST_OBJECTS] = {
 	"foo\rbar\r",
 	"foo\r\nbar\r\n",
 	"foo\nbar\rboth\r\nreversed\n\ragain\nproblems\r",
-	"123\n\000\001\002\003\004abc\255\254\253\r\n"
+	"123\n\000\001\002\003\004abc\255\254\253\r\n",
+	"\xEF\xBB\xBFThis is UTF-8\n",
+	"\xFE\xFF\x00T\x00h\x00i\x00s\x00!"
 };
-static git_off_t g_len[NUM_TEST_OBJECTS] = { -1, -1, -1, -1, -1, 17 };
-static git_text_stats g_stats[NUM_TEST_OBJECTS] = {
-	{ 0, 0, 0, 0, 0, 0 },
-	{ 0, 0, 2, 0, 6, 0 },
-	{ 0, 2, 0, 0, 6, 0 },
-	{ 0, 2, 2, 2, 6, 0 },
-	{ 0, 4, 4, 1, 31, 0 },
-	{ 1, 1, 2, 1, 9, 5 }
+static git_off_t g_len[NUM_TEST_OBJECTS] = { -1, -1, -1, -1, -1, 17, -1, 12 };
+static git_buf_text_stats g_stats[NUM_TEST_OBJECTS] = {
+	{ 0, 0, 0, 0, 0, 0, 0 },
+	{ 0, 0, 0, 2, 0, 6, 0 },
+	{ 0, 0, 2, 0, 0, 6, 0 },
+	{ 0, 0, 2, 2, 2, 6, 0 },
+	{ 0, 0, 4, 4, 1, 31, 0 },
+	{ 0, 1, 1, 2, 1, 9, 5 },
+	{ GIT_BOM_UTF8, 0, 0, 1, 0, 16, 0 },
+	{ GIT_BOM_UTF16_BE, 5, 0, 0, 0, 7, 5 },
 };
 static git_buf g_crlf_filtered[NUM_TEST_OBJECTS] = {
 	{ "", 0, 0 },
@@ -29,7 +33,9 @@ static git_buf g_crlf_filtered[NUM_TEST_OBJECTS] = {
 	{ "foo\rbar\r", 0, 8 },
 	{ "foo\nbar\n", 0, 8 },
 	{ "foo\nbar\rboth\nreversed\n\ragain\nproblems\r", 0, 38 },
-	{ "123\n\000\001\002\003\004abc\255\254\253\n", 0, 16 }
+	{ "123\n\000\001\002\003\004abc\255\254\253\n", 0, 16 },
+	{ "\xEF\xBB\xBFThis is UTF-8\n", 0, 17 },
+	{ "\xFE\xFF\x00T\x00h\x00i\x00s\x00!", 0, 12 }
 };
 
 void test_object_blob_filter__initialize(void)
@@ -76,12 +82,12 @@ void test_object_blob_filter__stats(void)
 	int i;
 	git_blob *blob;
 	git_buf buf = GIT_BUF_INIT;
-	git_text_stats stats;
+	git_buf_text_stats stats;
 
 	for (i = 0; i < NUM_TEST_OBJECTS; i++) {
 		cl_git_pass(git_blob_lookup(&blob, g_repo, &g_oids[i]));
 		cl_git_pass(git_blob__getbuf(&buf, blob));
-		git_text_gather_stats(&stats, &buf);
+		git_buf_text_gather_stats(&stats, &buf, false);
 		cl_assert(memcmp(&g_stats[i], &stats, sizeof(stats)) == 0);
 		git_blob_free(blob);
 	}