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

fmt: %q with byte value #63646

Closed
3052 opened this issue Oct 20, 2023 · 2 comments
Closed

fmt: %q with byte value #63646

3052 opened this issue Oct 20, 2023 · 2 comments
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@3052
Copy link

3052 commented Oct 20, 2023

If I run this code:

package main

import "fmt"

func main() {
   fmt.Printf("%s\n", "\xA1") 
   fmt.Printf("%q\n", "\xA1") 
   fmt.Printf("%c\n", rune(0xA1)) 
   fmt.Printf("%q\n", rune(0xA1)) 
   fmt.Printf("%c\n", byte(0xA1)) 
   fmt.Printf("%q\n", byte(0xA1)) 
}

I get this result:

�
"\xa1"
¡
'¡'
¡
'¡'

everything seems OK except for the last two items. INVERTED EXCLAMATION MARK (U+00A1) is actually 0xC2 0xA1 in UTF-8, not just 0xA1. with %c, this is explained in the documentation:

go/src/fmt/doc.go

Lines 26 to 33 in 693def1

Integer:
%b base 2
%c the character represented by the corresponding Unicode code point
%d base 10
%o base 8
%O base 8 with 0o prefix
%q a single-quoted character literal safely escaped with Go syntax.

but %q doesn't seem to be clear enough about what its doing. it seems with both %c and %q, a byte value is first converted to a rune before printing, but only %c draws attention to that fact. I think the current behavior is correct, but the %q value documentation should be more explicit about the conversion to a rune/Unicode code point.

@3052
Copy link
Author

3052 commented Oct 20, 2023

looks like this is related to 5 here:

https://go.dev/ref/spec#Conversions_to_and_from_a_string_type

as a workaround, you can use a byte slice instead:

package main

import "fmt"

func main() {
   fmt.Printf("%s\n", []byte{0xA1}) // �
   fmt.Printf("%q\n", []byte{0xA1}) // "\xa1"
}

@seankhliao seankhliao added Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Oct 21, 2023
@raghvenders
Copy link
Contributor

raghvenders commented Oct 26, 2023

Inverted exclamation is within a byte and hope the below helps


package main

import "fmt"

func main() {
	fmt.Printf("%s\n", "\xC2\xA1")
	fmt.Printf("%q\n", "\xC2\xA1")
	fmt.Printf("%+q\n", "\xC2\xA1")
	fmt.Printf("%#U\n", rune(0xA1))

}

@3052 3052 closed this as completed Oct 29, 2023
@3052 3052 reopened this Oct 29, 2023
@3052 3052 closed this as completed Nov 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants