-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_ptr_test.go
More file actions
106 lines (93 loc) · 2.93 KB
/
string_ptr_test.go
File metadata and controls
106 lines (93 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package fmt
import (
std_fmt "fmt"
"testing"
)
func TestStringPointer(t *testing.T) {
tests := []struct {
name string
initialValue string
transform func(*Conv) *Conv
expectedValue string
}{
{
name: "Remove tildes from string pointer",
initialValue: "áéíóúÁÉÍÓÚ",
transform: func(t *Conv) *Conv {
return t.Tilde()
},
expectedValue: "aeiouAEIOU",
},
{
name: "Convert to lowercase with string pointer",
initialValue: "HELLO WORLD",
transform: func(t *Conv) *Conv {
return t.ToLower()
},
expectedValue: "hello world",
},
{
name: "Convert to camelCase with string pointer",
initialValue: "hello world example",
transform: func(t *Conv) *Conv {
return t.CamelLow()
},
expectedValue: "helloWorldExample",
},
{
name: "Multiple transforms with string pointer",
initialValue: "Él Múrcielago Rápido",
transform: func(t *Conv) *Conv {
return t.Tilde().CamelLow()
},
expectedValue: "elMurcielagoRapido",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create string pointer with initial value
originalPtr := tt.initialValue
// Convert using string pointer and apply changes
tt.transform(Convert(&originalPtr)).Apply()
// Check if original pointer was updated correctly
if originalPtr != tt.expectedValue {
t.Errorf("\noriginalPtr = %q\nwant %q", originalPtr, tt.expectedValue)
}
})
}
}
// Estos ejemplos ilustran cómo usar los punteros a strings para evitar asignaciones adicionales
func Example_stringPointerBasic() {
// Creamos una variable string que queremos modificar
myText := "héllô wórld"
// En lugar de crear una nueva variable con el resultado,
// modificamos directamente la variable original usando Apply()
Convert(&myText).Tilde().ToLower().Apply()
// La variable original ha sido modificada
std_fmt.Println(myText)
// Output: hello world
}
func Example_stringPointerCamelCase() {
// Ejemplo de uso con múltiples transformaciones
originalText := "Él Múrcielago Rápido"
// Las transformaciones modifican la variable original directamente
// usando el método Apply() para actualizar el puntero
Convert(&originalText).Tilde().CamelLow().Apply()
std_fmt.Println(originalText)
// Output: elMurcielagoRapido
}
func Example_stringPointerEfficiency() {
// En aplicaciones de alto rendimiento, reducir asignaciones de memoria
// puede ser importante para evitar la presión sobre el garbage collector
// Método tradicional (crea nuevas asignaciones de memoria)
traditionalText := "Texto con ACENTOS"
processedText := Convert(traditionalText).Tilde().ToLower().String()
std_fmt.Println(processedText)
// Método con punteros (modifica directamente la variable original)
directText := "Otro TEXTO con ACENTOS"
Convert(&directText).Tilde().ToLower().Apply()
std_fmt.Println(directText)
// Output:
// texto con acentos
// otro texto con acentos
}