Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/text/width: lose one character #14817

Closed
naduma opened this issue Mar 14, 2016 · 3 comments
Closed

x/text/width: lose one character #14817

naduma opened this issue Mar 14, 2016 · 3 comments

Comments

@naduma
Copy link

naduma commented Mar 14, 2016

When Fold.String/Narrow.String was called, it was lost one character.

want := "A" + strings.Repeat("0", 127) + "B"
got := width.Fold.String("\uff21" + strings.Repeat("0", 127) + "B")
=> got != want, "B" is lost.

conditions:

  • long string (more than 128 [initialBufSize])
  • contains wide character

initialBufSize from https://github.com/golang/text/blob/master/transform/transform.go#L506

cause:

https://github.com/golang/text/blob/master/width/transform.go#L25

Even when the length of "dst" is 0, "nSrc" increases one.

InitStmt(nSrc++) is necessary?

@bradfitz
Copy link
Contributor

Please post a complete and minimal program demonstrating the problem, not just a snippet. Is the \uff21 necessary for the repro? Ideally please provide a share link from https://play.golang.org/ where it reproduces online.

InitStmt(nSrc++) is not valid Go. ++ is not an expression in Go.

@naduma
Copy link
Author

naduma commented Mar 15, 2016

$ go version
go version go1.6 linux/amd64

Steps to Reproduce

$ go get "golang.org/x/text"

$ go run sample.go

sample.go

package main

import (
    "fmt"
    "strings"

    "golang.org/x/text/width"
)

func main() {
    want := "A" + strings.Repeat("0", 127) + "B"
    got := width.Fold.String("\uff21" + strings.Repeat("0", 127) + "B")
    if got != want {
        fmt.Println("Fold.String NG!")
        fmt.Printf("got  %s (%d)\n", got, len(got))
        fmt.Printf("want %s (%d)\n", want, len(want))
    } else {
        fmt.Println("Fold.String OK!")
    }

    want = "A" + strings.Repeat("0", 127) + "B"
    got = width.Narrow.String("\uff21" + strings.Repeat("0", 127) + "B")
    if got != want {
        fmt.Println("Narrow.String NG!")
        fmt.Printf("got  %s (%d)\n", got, len(got))
        fmt.Printf("want %s (%d)\n", want, len(want))
    } else {
        fmt.Println("Narrow.String OK!")
    }
}

InitStmt(nSrc++) is not valid Go. ++ is not an expression in Go.

InitStm is ForClause's InitStmt. ex. for i:=0; i < 10; i++ {

I'm not good at English. I wrote a patch sample, please check it.

https://gist.github.com/naduma/90a8fa3e21fffa1bbf80

@bradfitz bradfitz changed the title text/width: lose one character x/text/width: lose one character Apr 3, 2016
@bradfitz bradfitz added this to the Unreleased milestone Apr 3, 2016
@gopherbot
Copy link

CL https://golang.org/cl/21500 mentions this issue.

@golang golang locked and limited conversation to collaborators Apr 8, 2017
@rsc rsc unassigned mpvl Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants