threads / patch / 59384

patchtest: encoding: simplify BOM addition

Subject: [PATCH] test: encoding: simplify BOM addition

## tl;dr

One message between Mar 13, 2023 and Mar 13, 2023. Diffs are folded; open one to read it.

replies: 0people: 1as markdown or json

Felipe Contreras· Mar 13, 2023, 05:04 UTC · lore

In 79444c9294 (utf8: handle systems that don't write BOM for UTF-16, 2019-02-12) some prereqs were added (NO_UTF16_BOM and NO_UTF32_BOM) in order to conditionally add BOMs for the systems that need them.

But we don't need to check that prereq every time we are going to write some text.

Let's check the requirement only once and generate write_utf{16,32} accordingly.

Cc: Brian M. Carlson <sandals@crustytoothpaste.net>
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 t/lib-encoding.sh | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
Show changes to t/lib-encoding.sh +14 −14
diff --git a/t/lib-encoding.sh b/t/lib-encoding.sh
index 2dabc8c73e..15aea597be 100644
--- a/t/lib-encoding.sh
+++ b/t/lib-encoding.sh
@@ -1,25 +1,25 @@
 # Encoding helpers
 
-test_lazy_prereq NO_UTF16_BOM '
-	test $(printf abc | iconv -f UTF-8 -t UTF-16 | wc -c) = 6
-'
+if test "$(printf abc | iconv -f UTF-8 -t UTF-16 | wc -c)" = 6
+then
+	add_utf16_bom () { printf '\376\377'; }
+else
+	add_utf16_bom () { :; }
+fi
 
-test_lazy_prereq NO_UTF32_BOM '
-	test $(printf abc | iconv -f UTF-8 -t UTF-32 | wc -c) = 12
-'
+if test "$(printf abc | iconv -f UTF-8 -t UTF-32 | wc -c)" = 12
+then
+	add_utf32_bom () { printf '\0\0\376\377'; }
+else
+	add_utf32_bom () { :; }
+fi
 
 write_utf16 () {
-	if test_have_prereq NO_UTF16_BOM
-	then
-		printf '\376\377'
-	fi &&
+	add_utf16_bom &&
 	iconv -f UTF-8 -t UTF-16
 }
 
 write_utf32 () {
-	if test_have_prereq NO_UTF32_BOM
-	then
-		printf '\0\0\376\377'
-	fi &&
+	add_utf32_bom &&
 	iconv -f UTF-8 -t UTF-32
 }
-- 
2.39.2.13.g1fb56cf030

← back to recent threads