-
Notifications
You must be signed in to change notification settings - Fork 96
Add length check of the string #48
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
import ( | ||
"fmt" | ||
"unicode/utf8" | ||
) | ||
|
||
const minStrLen = 7 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, feel free to rewrite the entire example!
|
||
fmt.Printf("Before: %s\n", s); | ||
fmt.Printf("After : %s\n", string(r)) | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.