Skip to content

UTF-8 Unicode Safe Golang Byte Size WordWrap

License

Notifications You must be signed in to change notification settings

donatj/wordwrap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WordWrap

Go Report Card CI GoDoc

UTF-8 Grapheme Cluster Safe Word Wrapping / Line Splitting for Go based on number of bytes.

This library wraps text without breaking UTF-8 grapheme clusters. It operates on byte count, not runes. It breaks on whitespace first. If a word is too long, it breaks between grapheme clusters. It never splits emojis like 👩‍👩‍👧‍👧 or characters with combining marks.

This is useful for protocols where message size is limited by bytes.

Samples

English:

// import "log"
wrapped, err := wordwrap.WrapString(
	`If any earl, baron, or other person that holds lands directly of the Crown, for military service, shall die, and at his death his heir shall be of full age and owe a 'relief', the heir shall have his inheritance on payment of the ancient scale of 'relief'.`,
	60)
if err != nil {
	log.Fatal(err)
}
fmt.Println(wrapped)

Becomes:

If any earl, baron, or other person that holds lands         // 53 bytes
directly of the Crown, for military service, shall die, and  // 60 bytes
at his death his heir shall be of full age and owe a         // 53 bytes
'relief', the heir shall have his inheritance on payment of  // 60 bytes
the ancient scale of 'relief'.                               // 30 bytes

Japanese:

// import "log"
wrapped, err := wordwrap.WrapString(
	`クラウンの直接土地を保持している任意の伯爵、男爵、または他の人は、兵役のために、死ぬ、と彼の死で彼の後継者は成年であることと「救済」を借りなければならない場合は、相続人は、支払いの彼の継承をもたなければなりません「救済」の古代規模の。`,
	60)
if err != nil {
	log.Fatal(err)
}
fmt.Println(wrapped)

Becomes:

クラウンの直接土地を保持している任意の伯  // 60 bytes
爵、男爵、または他の人は、兵役のために、  // 60 bytes
死ぬ、と彼の死で彼の後継者は成年であるこ  // 60 bytes
とと「救済」を借りなければならない場合は  // 60 bytes
、相続人は、支払いの彼の継承をもたなけれ  // 60 bytes
ばなりません「救済」の古代規模の。       // 51 bytes

Korean:

// import "log"
wrapped, err := wordwrap.WrapString(
	`크라운 의 직접 토지 를 보유하고 있는 백작 , 남작 , 또는 다른 사람이 군 복무 를 위해 죽을 것이요, 그의 죽음 에 그의 후계자 가 전체 연령 하고' 구호 '을 빚을 해야 하는 경우, 상속인 이 지불 에 대한 자신의 상속을 가져야한다 ' 구호 ' 의 고대 규모의 `,
	60)
if err != nil {
	log.Fatal(err)
}
fmt.Println(wrapped)

Becomes:

크라운 의 직접 토지 를 보유하고 있는 백작   // 59 bytes
, 남작 , 또는 다른 사람이 군 복무 를 위해  // 57 bytes
죽을 것이요, 그의 죽음 에 그의 후계자 가   // 57 bytes
전체 연령 하고' 구호 '을 빚을 해야 하는    // 55 bytes
경우, 상속인 이 지불 에 대한 자신의 상속을 // 60 bytes
가져야한다 ' 구호 ' 의 고대 규모의       // 47 bytes

Grapheme Clusters:

// import "log"
wrapped, err := wordwrap.WrapString(
	`Hello 👩‍👩‍👧‍👧 family 🧑‍🎄 celebrating café with naïve résumé क्षि`, 
	30)
if err != nil {
	log.Fatal(err)
}
fmt.Println(wrapped)

Becomes:

Hello                                 // 6 bytes
👩‍👩‍👧‍👧                                    // 26 bytes
family 🧑‍🎄                             // 19 bytes
celebrating café with naïve           // 30 bytes
résumé क्षि                             // 21 bytes

Error Handling

The library returns an ErrGraphemeClusterTooLarge error when a grapheme cluster exceeds the byte limit. This allows you to handle these situations gracefully.

Single Japanese character on 2-byte limit:

// import "errors"
_, err := wordwrap.SplitString("し", 2)  // error: し is 3 bytes
if errors.Is(err, wordwrap.ErrGraphemeClusterTooLarge) {
	// Handle the error
}

Family emoji on 20-byte limit:

// import "errors"
_, err := wordwrap.SplitString("👩‍👩‍👧‍👧", 20)  // error: emoji is 25 bytes
if errors.Is(err, wordwrap.ErrGraphemeClusterTooLarge) {
	// Handle the error
}

Person with tree on 8-byte limit:

// import "errors"
_, err := wordwrap.SplitString("🧑‍🎄", 8)  // error: emoji is 11 bytes
if errors.Is(err, wordwrap.ErrGraphemeClusterTooLarge) {
	// Handle the error
}

String ending with oversized cluster:

// import "errors"
_, err := wordwrap.SplitString("test 👩‍👩‍👧‍👧", 20)  // error: emoji is 25 bytes
if errors.Is(err, wordwrap.ErrGraphemeClusterTooLarge) {
	// Handle the error
}

About

UTF-8 Unicode Safe Golang Byte Size WordWrap

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages