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

String to []rune conversion #406

Closed
wants to merge 3 commits into from

Conversation

scriptonist
Copy link
Contributor

This PR is aimed at Solving/Fixing #69, I consider this change to be aimed at getting some initial feedback. I'm new to the project and compilers in general. So as the CONTRIBUTING.md suggests, please be kind 😅

This change is heavily inspired/copied from the go source.

Copy link
Member

@aykevl aykevl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. However, can you add a few tests that tests various strings (including Unicode)?
You can add them to testdata/slice.go, or perhaps even better rename testdata/rangestring.* to testdata/string.* and add them there. You can test with go test as usual. The output of these Go files must must match the .txt files with the same name in the testdata/ directory. To test out changes to the test files, you can run them with tinygo run testdata/slice.go for example.

@@ -2473,6 +2473,8 @@ func (c *Compiler) parseConvert(typeFrom, typeTo types.Type, value llvm.Value, p
switch elemType.Kind() {
case types.Byte:
return c.createRuntimeCall("stringToBytes", []llvm.Value{value}, ""), nil
case types.Rune:
return c.createRuntimeCall("stringToRunes", []llvm.Value{value}, ""), nil
default:
return llvm.Value{}, c.makeError(pos, "todo: convert from string: "+elemType.String())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both cases allowed by the Go spec are now supported, so it's a bug in the compiler if we get here. You can replace the todo: error with a panic. (Note: getting here would be a bug because when we get to SSA level the code has long been type checked and has already been verified as being valid Go).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was really not sure what message to put there, please help me with that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about unexpected type in string to slice conversion? It's not hugely important, but it's useful if it is somewhat descriptive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I've made the suggested changes. Can you take a look? @aykevl

n++
}
var r = make([]rune, n)
n = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you make the change I suggest above, you can remove this line entirely.

}
var r = make([]rune, n)
n = 0
for _, e := range s {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like you could replace the range here with:

for i := 0; i < n; i++ {
	r[i] = s[i]
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately that doesn't work, indexing into a string returns a byte, not a rune.

@deadprogram
Copy link
Member

Thanks for the making some changes to this PR @scriptonist I also made a few suggestions.

Can you please add a test that uses a unicode string? Any special characters, such as this example from the golang.org web site, would be sufficient:

const nihongo = "日本語"

Thank you.

@aykevl
Copy link
Member

aykevl commented Jun 17, 2019

@deadprogram there is already a test included in this PR and it is actually more extensive than testing a few Chinese (?) characters because it tests various UTF-8 byte lengths (1, 2, 3, and 4 byte characters).

@scriptonist
Copy link
Contributor Author

@deadprogram @aykevl Should I add something here, It looked like a NOP for me from the comments.

@aykevl
Copy link
Member

aykevl commented Jun 18, 2019

Thank you for this addition! I have merged the PR in e9f6e51 but modified the commit message to match the style of other commit messages (we follow Go commit message conventions).

Sorry it took so long, I got really busy the last few days.

@aykevl aykevl closed this Jun 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants