MCPcopy Create free account
hub / github.com/imgproxy/imgproxy / replaceEntities

Function replaceEntities

xmlparser/entities.go:67–123  ·  view source on GitHub ↗
(
	data T,
	em map[string][]byte,
	indexFn func(T, byte) int,
)

Source from the content-addressed store, hash-verified

65}
66
67func replaceEntities[T string | []byte](
68 data T,
69 em map[string][]byte,
70 indexFn func(T, byte) int,
71) T {
72 var replaced []byte
73
74 searchInd := 0
75
76 for {
77 // Find the entity start
78 i := indexFn(data[searchInd:], '&')
79 if i == -1 {
80 break
81 }
82 i += searchInd
83
84 // Find the entity end
85 j := indexFn(data[i:], ';')
86 if j == -1 {
87 break
88 }
89 j += i
90
91 // Get the entity name
92 name := data[i+1 : j]
93
94 // Find the replacement value
95 if val, ok := em[string(name)]; ok {
96 // If this is the first replacement, prealloc the replaced slice
97 if len(replaced) == 0 {
98 replaced = make([]byte, 0, len(data))
99 }
100
101 // Append the data before the entity and the replacement value
102 replaced = append(replaced, data[:i]...)
103 replaced = append(replaced, val...)
104
105 // Move the data pointer forward and reset search index
106 data = data[j+1:]
107 searchInd = 0
108 } else {
109 // Didn't find replacement, just move the search index forward
110 searchInd = j + 1
111 }
112 }
113
114 // If no replacements were made, return the original data
115 if len(replaced) == 0 {
116 return data
117 }
118
119 // Append any remaining data after the last entity
120 replaced = append(replaced, data...)
121
122 return T(replaced)
123}

Callers 2

ReplaceEntitiesBytesFunction · 0.85
ReplaceEntitiesStringFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected