Golang UTF-8 coding Name Attribute #5

Closed
opened 2021-07-22 18:27:26 +00:00 by erwan · 0 comments
Member

Golang string base on an econdage ASCII. So the string don't accept the "accent" like 'éêâîûôàè'.

To resolve this problem we need to encode our name in UTF-8 through Rune in golang. The officiel documentation of Rune in golang.

Code generateName:

func generateName(r *rand.Rand) (name string) {
	alphabet := "abcé"
	log.Debug(alphabet)
	for only_one := true; only_one; _, only_one = all_names[name] {
		for i := minimal_length; i < maximal_length; i++ {
			name += string(alphabet[r.Intn(len(alphabet))])
		}

	}
	all_names[name] = struct{}{}
	log.Debug(fmt.Sprintf("Name generated: %s.
", name))
	return
}

Result:

DEBU[0000] Name generated: ©cÃba©aÃcbac.                
DEBU[0000] abcé                                         
DEBU[0000] Name generated: ©cbÃÃa©ÃcÃÃa.                
DEBU[0000] abcé                                         
DEBU[0000] Name generated: ac©©a©©©c©c©.                
INFO[0000] Created random users succes: 5  

Without the for loop:

func generateName(r *rand.Rand) (name string) {
	alphabet := "abcéâîûèôâà à"
	log.Debug(alphabet)
	length := r.Intn(maximal_length)
	//for only_one := true; only_one; _, only_one = all_names[name] {
	//	for i := minimal_length; i < length; i++ {
	//		name += string(alphabet[r.Intn(len(alphabet))])

	//	}

	//}
	name = alphabet
	all_names[name] = struct{}{}
	log.Debugf("Len: %d, Rune: %d, Length: %d", len(name), rune.RuneCountInString(name), length)
	log.Debug(fmt.Sprintf("Name generated: %s.
", name))
	return
}

Result:

INFO[0000] Created random groups succes: 1              
DEBU[0000] abcéâîûèôâà à                                
DEBU[0000] Len: 22, Rune: 13, Length: 10                
DEBU[0000] Name generated: abcéâîûèôâà à.               
DEBU[0000] abcéâîûèôâà à                                
DEBU[0000] Len: 22, Rune: 13, Length: 2                 
DEBU[0000] Name generated: abcéâîûèôâà à.               
INFO[0000] Created random users succes: 1

On Consul we don't have any problems. It works perfectly.

Error from:

The error is from our alphabet[], it takes only 1 byte but éèàî is encoding on several bytes. After we transform this byte in type String.
We can't remove the slice and transform this in Map with int in key and string in value. After we concat the two strings. Like this we don't have any problems.

Golang string base on an econdage ASCII. So the string don't accept the "accent" like 'éêâîûôàè'. To resolve this problem we need to encode our name in UTF-8 through Rune in golang. The officiel documentation of [Rune](https://pkg.go.dev/unicode/utf8#example-EncodeRune) in golang. ### Code generateName: ```go func generateName(r *rand.Rand) (name string) { alphabet := "abcé" log.Debug(alphabet) for only_one := true; only_one; _, only_one = all_names[name] { for i := minimal_length; i < maximal_length; i++ { name += string(alphabet[r.Intn(len(alphabet))]) } } all_names[name] = struct{}{} log.Debug(fmt.Sprintf("Name generated: %s. ", name)) return } ``` ### Result: ``` DEBU[0000] Name generated: ©cÃba©aÃcbac. DEBU[0000] abcé DEBU[0000] Name generated: ©cbÃÃa©ÃcÃÃa. DEBU[0000] abcé DEBU[0000] Name generated: ac©©a©©©c©c©. INFO[0000] Created random users succes: 5 ``` ### Without the for loop: ```go func generateName(r *rand.Rand) (name string) { alphabet := "abcéâîûèôâà à" log.Debug(alphabet) length := r.Intn(maximal_length) //for only_one := true; only_one; _, only_one = all_names[name] { // for i := minimal_length; i < length; i++ { // name += string(alphabet[r.Intn(len(alphabet))]) // } //} name = alphabet all_names[name] = struct{}{} log.Debugf("Len: %d, Rune: %d, Length: %d", len(name), rune.RuneCountInString(name), length) log.Debug(fmt.Sprintf("Name generated: %s. ", name)) return } ``` #### Result: ``` INFO[0000] Created random groups succes: 1 DEBU[0000] abcéâîûèôâà à DEBU[0000] Len: 22, Rune: 13, Length: 10 DEBU[0000] Name generated: abcéâîûèôâà à. DEBU[0000] abcéâîûèôâà à DEBU[0000] Len: 22, Rune: 13, Length: 2 DEBU[0000] Name generated: abcéâîûèôâà à. INFO[0000] Created random users succes: 1 ``` On Consul we don't have any problems. It works perfectly. ### Error from: The error is from our `alphabet[]`, it takes only 1 byte but `éèàî` is encoding on several bytes. After we transform this byte in type String. We can't remove the slice and transform this in Map with `int` in key and `string` in value. After we concat the two strings. Like this we don't have any problems.
lx closed this issue 2021-07-29 08:02:29 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: Deuxfleurs/bottin#5
No description provided.