開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Go (プログラミング言語)
プログラミング言語Go (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES) (Alan A.A. Donovan(著)、Brian W. Kernighan(著)、柴田 芳樹(翻訳)、丸善出版)の第4章(コンポジット型)、4.2(スライス)、4.2.2(スライス内での技法)、練習問題4.6の解答を求めてみる。
コード
package main
import (
"fmt"
"unicode"
)
func main() {
bss := [][]byte{
[]byte(""),
[]byte("a"),
[]byte(" a"),
[]byte("a "),
[]byte(" a"),
[]byte("a "),
[]byte("a b"),
[]byte("a b"),
[]byte(" a b c"),
[]byte(" 日 a あ\n\t "),
}
for _, bs := range bss {
fmt.Printf("%q\n", bs)
fmt.Printf("%q\n\n", remove(bs))
}
}
func remove(bs []byte) []byte {
out := bs[:0]
rs := []rune(string(bs))
isSpace := false
for _, r := range rs {
if isSpace && !unicode.IsSpace(r) {
isSpace = false
out = append(out, ' ')
for _, b := range []byte(string(r)) {
out = append(out, b)
}
} else if unicode.IsSpace(r) {
isSpace = true
} else {
for _, b := range []byte(string(r)) {
out = append(out, b)
}
}
}
if isSpace {
out = append(out, ' ')
}
return out
}
入出力結果(cmd(コマンドプロンプト)、Terminal)
C:\Users\...>go run sample6.go "" "" "a" "a" " a" " a" "a " "a " " a" " a" "a " "a " "a b" "a b" "a b" "a b" " a b c" " a b c" "\u3000\u3000 日\u3000a あ\n\t\u3000" " 日 a あ " C:\Users\...>
0 コメント:
コメントを投稿