Source file test/codegen/strings.go
1 // asmcheck 2 3 // Copyright 2018 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 package codegen 8 9 import "strings" 10 11 // This file contains code generation tests related to the handling of 12 // string types. 13 14 func CountRunes(s string) int { // Issue #24923 15 // amd64:`.*countrunes` 16 return len([]rune(s)) 17 } 18 19 func CountBytes(s []byte) int { 20 // amd64:-`.*runtime.slicebytetostring` 21 return len(string(s)) 22 } 23 24 func ToByteSlice() []byte { // Issue #24698 25 // amd64:`LEAQ type:\[3\]uint8` 26 // amd64:`CALL runtime\.(newobject|mallocgcTinySize3)` 27 // amd64:-`.*runtime.stringtoslicebyte` 28 return []byte("foo") 29 } 30 31 func ConvertToByteSlice(a, b, c string) []byte { 32 // amd64:`.*runtime.concatbyte3` 33 return []byte(a + b + c) 34 } 35 36 // Loading from read-only symbols should get transformed into constants. 37 func ConstantLoad() { 38 // 12592 = 0x3130 39 // 50 = 0x32 40 // amd64:`MOVW \$12592, \(`,`MOVB \$50, 2\(` 41 // 386:`MOVW \$12592, \(`,`MOVB \$50, 2\(` 42 // arm:`MOVW \$48`,`MOVW \$49`,`MOVW \$50` 43 // arm64:`MOVD \$12592`,`MOVD \$50` 44 // loong64:`MOVV \$12592`,`MOVV \$50` 45 // wasm:`I64Const \$12592`,`I64Store16 \$0`,`I64Const \$50`,`I64Store8 \$2` 46 // mips64:`MOVV \$48`,`MOVV \$49`,`MOVV \$50` 47 bsink = []byte("012") 48 49 // 858927408 = 0x33323130 50 // 13620 = 0x3534 51 // amd64:`MOVL \$858927408`,`MOVW \$13620, 4\(` 52 // 386:`MOVL \$858927408`,`MOVW \$13620, 4\(` 53 // arm64:`MOVD \$858927408`,`MOVD \$13620` 54 // loong64:`MOVV \$858927408`,`MOVV \$13620` 55 // wasm:`I64Const \$858927408`,`I64Store32 \$0`,`I64Const \$13620`,`I64Store16 \$4` 56 bsink = []byte("012345") 57 58 // 3978425819141910832 = 0x3736353433323130 59 // 7306073769690871863 = 0x6564636261393837 60 // amd64:`MOVQ \$3978425819141910832`,`MOVQ \$7306073769690871863` 61 // 386:`MOVL \$858927408, \(`,`DUFFCOPY` 62 // arm64:`MOVD \$3978425819141910832`,`MOVD \$7306073769690871863`,`MOVD \$15` 63 // loong64:`MOVV \$3978425819141910832`,`MOVV \$7306073769690871863`,`MOVV \$15` 64 // wasm:`I64Const \$3978425819141910832`,`I64Store \$0`,`I64Const \$7306073769690871863`,`I64Store \$7` 65 bsink = []byte("0123456789abcde") 66 67 // 56 = 0x38 68 // amd64:`MOVQ \$3978425819141910832`,`MOVB \$56` 69 // loong64:`MOVV \$3978425819141910832`,`MOVV \$56` 70 bsink = []byte("012345678") 71 72 // 14648 = 0x3938 73 // amd64:`MOVQ \$3978425819141910832`,`MOVW \$14648` 74 // loong64:`MOVV \$3978425819141910832`,`MOVV \$14648` 75 bsink = []byte("0123456789") 76 77 // 1650538808 = 0x62613938 78 // amd64:`MOVQ \$3978425819141910832`,`MOVL \$1650538808` 79 // loong64:`MOVV \$3978425819141910832`,`MOVV \$1650538808` 80 bsink = []byte("0123456789ab") 81 } 82 83 // self-equality is always true. See issue 60777. 84 func EqualSelf(s string) bool { 85 // amd64:`MOVL \$1, AX`,-`.*memequal.*` 86 return s == s 87 } 88 func NotEqualSelf(s string) bool { 89 // amd64:`XORL AX, AX`,-`.*memequal.*` 90 return s != s 91 } 92 93 var bsink []byte 94 95 func HasPrefix3(s string) bool { 96 // amd64:-`.*memequal.*` 97 return strings.HasPrefix(s, "str") 98 } 99 100 func HasPrefix5(s string) bool { 101 // amd64:-`.*memequal.*` 102 return strings.HasPrefix(s, "strin") 103 } 104 105 func HasPrefix6(s string) bool { 106 // amd64:-`.*memequal.*` 107 return strings.HasPrefix(s, "string") 108 } 109 110 func HasPrefix7(s string) bool { 111 // amd64:-`.*memequal.*` 112 return strings.HasPrefix(s, "strings") 113 } 114