Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

Add length check of the string #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions ex-basics/src/string3.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package main

import (
"fmt"
)

func main() {
s := "Шла Саша по шоссе"
r := []rune(s)
copy(r[4:4+3], []rune("abc"))
fmt.Printf("Before: %s\n", s);
fmt.Printf("After : %s\n", string(r))
}
package main
Copy link
Owner

Choose a reason for hiding this comment

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

Strange diff, that seems to replace the entire file?

Copy link
Author

Choose a reason for hiding this comment

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

Very sorry for it. Maybe this is caused by my editor setting, and I will check it carefully before committing code next time.


import (
"fmt"
"unicode/utf8"
)

const minStrLen = 7
Copy link
Owner

Choose a reason for hiding this comment

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

I don't like this explicit length const


func main() {
s := "Шла Саша по шоссе"
r := []rune(s)
if utf8.RuneCount([]byte(s)) < minStrLen {
fmt.Printf("The length of string is shorter than %d\n", minStrLen)
return
}
copy(r[4:4+3], []rune("abc"))
Copy link
Owner

Choose a reason for hiding this comment

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

Try to come up with a way to dynamically check the bounds (i.ie. for any sizes string)

Copy link
Author

Choose a reason for hiding this comment

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

Personally, I think it is a not good programming habit to use hardcode (copy(r[4:4+3], []rune("abc"))), and it seems OK just as an example.

Copy link
Owner

Choose a reason for hiding this comment

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

True, feel free to rewrite the entire example!
On 21 Nov 2013 08:40, "NanXiao" [email protected] wrote:

In ex-basics/src/string3.go:

+import (

  • "fmt"
  • "unicode/utf8"
    +)

+const minStrLen = 7
+
+func main() {

  • s := "Шла Саша по шоссе"
  • r := []rune(s)
  • if utf8.RuneCount([]byte(s)) < minStrLen {
  •   fmt.Printf("The length of string is shorter than %d\n", minStrLen)
    
  •   return
    
  • }
  • copy(r[4:4+3], []rune("abc"))

Personally, I think it is a not good programming habit to use hardcode
(copy(r[4:4+3], []rune("abc"))), and it seems OK just as an example.


Reply to this email directly or view it on GitHubhttps://github.com//pull/48/files#r7818169
.

fmt.Printf("Before: %s\n", s);
fmt.Printf("After : %s\n", string(r))
}