Skip to content

Commit 5ff97d9

Browse files
authored
Merge pull request #517 from devlights/add-parseint-tips
2 parents 959ddb8 + 9feab9d commit 5ff97d9

File tree

5 files changed

+100
-4
lines changed

5 files changed

+100
-4
lines changed

examples/basic/strconvs/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44

55
|file|example name|note|
66
|----|------------|----|
7-
|hex\_to\_decimal\_convert.go|hex\_to\_decimal\_convert|16進数文字列を10進数に変換するサンプルです.|
8-
7+
|hex\_to\_decimal.go|strconvs\_hex\_to\_decimal|16進数文字列を10進数に変換するサンプルです.|
8+
|bin\_to\_decimal.go|strconvs\_bin\_to\_decimal|2進数文字列を10進数に変換するサンプルです.|
9+
|parseint\_tips\_bitsize.go|strconvs\_parseint\_tips\_bitsize|strconv.ParseInt() の第3引数 bitSize を指定する際のTipsです。|
10+
|parseint\_tips\_basevalue.go|strconvs\_parseint\_tips\_base|strconv.ParseInt() の第2引数 base を指定する際のTipsです。|

examples/basic/strconvs/examples.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ func NewRegister() mapping.Register {
1515
func (r *register) Regist(m mapping.ExampleMapping) {
1616
m["strconvs_hex_to_decimal"] = HexToDecimal
1717
m["strconvs_bin_to_decimal"] = BinToDecimal
18+
m["strconvs_parseint_tips_bitsize"] = ParseIntTipsBitSize
19+
m["strconvs_parseint_tips_base"] = ParseIntTipsBaseValue
1820
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package strconvs
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// ParseIntTipsBaseValue -- strconv.ParseInt() の第2引数 base を指定する際のTipsです。
10+
//
11+
// REFERENCES
12+
// - https://pkg.go.dev/strconv@latest#ParseInt
13+
func ParseIntTipsBaseValue() error {
14+
//
15+
// strconv.ParseInt() の 第2引数 base には
16+
// 通常、元の文字列の進数を設定する。(2, 8, 10, 16 など)
17+
// この場合、0xff のように prefix を付けているとエラーとなる。
18+
//
19+
// しかし、base に 0 を指定した場合は prefix を付けていてもエラーにならない。
20+
// 2進数の場合は 0b 、8進数の場合は 0o 、16進数の場合は 0x となる。
21+
// この場合は逆に prefix が付いていないとエラーとなる。
22+
//
23+
// さらに、 parseint_tips_bitsize.go にあるように 第3引数の bitSize にも
24+
// 0 を指定することができる。
25+
//
26+
27+
var (
28+
values = []string{
29+
"0b11111111",
30+
"0o377",
31+
"0xff",
32+
}
33+
)
34+
35+
for _, v := range values {
36+
var (
37+
parsed int64
38+
err error
39+
)
40+
41+
parsed, err = strconv.ParseInt(v, 0, 0)
42+
if err != nil {
43+
return err
44+
}
45+
46+
output.Stdoutl("[original]", v)
47+
output.Stdoutl("[parsed ]", parsed)
48+
output.StdoutHr()
49+
}
50+
51+
return nil
52+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package strconvs
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// ParseIntTipsBitSize -- strconv.ParseInt() の第3引数 bitSize を指定する際のTipsです。
10+
//
11+
// REFERENCES
12+
// - https://pkg.go.dev/strconv@latest#ParseInt
13+
func ParseIntTipsBitSize() error {
14+
//
15+
// strconv.ParseInt() の 第3引数 bitSize には
16+
// 通常、値が収まるビットサイズを指定することになるが
17+
// 0を指定すると、 int に収まるようにしてくれる。
18+
//
19+
// 大抵の場合、intで取得したいときが多いので
20+
// 0を指定しておくと楽。
21+
//
22+
23+
var (
24+
value = "ff"
25+
base = 16
26+
bitSize = 0
27+
parsed int64
28+
err error
29+
)
30+
31+
parsed, err = strconv.ParseInt(value, base, bitSize)
32+
if err != nil {
33+
return err
34+
}
35+
36+
output.Stdoutl("[original]", value)
37+
output.Stdoutl("[parsed ]", int(parsed))
38+
39+
return nil
40+
}

examples/basic/strs/rune_count.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ func RuneCount() error {
1616
s1 = "h"
1717
s2 = "あ"
1818
s3 = "😺"
19-
s4 = "🧑‍🤝‍🧑" //lint:ignore ST1018 ok
20-
s5 = "👨‍👩‍👧‍👦" //lint:ignore ST1018 ok
19+
s4 = "🧑‍🤝‍🧑"
20+
s5 = "👨‍👩‍👧‍👦"
2121
)
2222

2323
var (

0 commit comments

Comments
 (0)