Skip to content

Instantly share code, notes, and snippets.

@eyasuyuki
Created January 30, 2023 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyasuyuki/bcc97f3a712dca926676701b9d65fe17 to your computer and use it in GitHub Desktop.
Save eyasuyuki/bcc97f3a712dca926676701b9d65fe17 to your computer and use it in GitHub Desktop.
(use srfi-13)
(use gauche.test)
(define (starts-with? str prefix)
(let ((str-len (string-length str))
(prefix-len (string-length prefix)))
(if (> str-len prefix-len)
(string=? (substring str 0 prefix-len) prefix)
#f)))
(define (remove-bom path)
(let* ((bom (string #xEF #xBB #xBF))
(content (slurp path)))
(if (starts-with? content bom)
(let ((new-content (substring content (string-length bom))))
(spit path new-content)))))
(define (walk path)
(if (file-directory? path)
(do-directory path walk)
(if (string-suffix? ".java" path)
(remove-bom path))))
(define (test-remove-bom path)
(let ((file (open path "wb")))
(write-string (string #xEF #xBB #xBF) file)
(write-string "hello" file)
(close file))
(remove-bom path)
(let ((content (slurp path)))
(assert-string= content "hello")))
(define-test-group remove-bom
(test-remove-bom "bom.txt"))
(run-tests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment