go-utf8/format.go

99 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package utf8
import (
"bytes"
)
// FormatBinary returns a representation of a rune as a sequence of bytes, given in binary format.
//
// Example
//
// utf8.FormatBinary('۵')
//
// // Outputs:
// // <<0b11011011 ; 0b10110101>>
func FormatBinary(r rune) string {
var buffer bytes.Buffer
_, err := WriteRune(&buffer, r)
if nil != err {
return "<<ERROR>>"
}
var output bytes.Buffer
length := buffer.Len()
for i:=0; i<length; i++ {
switch i {
case 0:
output.WriteString("<<0b")
default:
output.WriteString(" ; 0b")
}
b := buffer.Bytes()[i]
switch {
case 0 == (0x80 & b):
output.WriteRune('0')
default:
output.WriteRune('1')
}
switch {
case 0 == (0x40 & b):
output.WriteRune('0')
default:
output.WriteRune('1')
}
switch {
case 0 == (0x20 & b):
output.WriteRune('0')
default:
output.WriteRune('1')
}
switch {
case 0 == (0x10 & b):
output.WriteRune('0')
default:
output.WriteRune('1')
}
switch {
case 0 == (0x08 & b):
output.WriteRune('0')
default:
output.WriteRune('1')
}
switch {
case 0 == (0x04 & b):
output.WriteRune('0')
default:
output.WriteRune('1')
}
switch {
case 0 == (0x02 & b):
output.WriteRune('0')
default:
output.WriteRune('1')
}
switch {
case 0 == (0x01 & b):
output.WriteRune('0')
default:
output.WriteRune('1')
}
}
output.WriteString(">>")
return output.String()
}