github.com/aretext/aretext@v1.3.0/text/runestack.go (about) 1 package text 2 3 // RuneStack represents a string with efficient operations to push/pop runes. 4 // The zero value is equivalent to an empty string. 5 type RuneStack struct { 6 runes []rune 7 dirty bool 8 cached string 9 } 10 11 func (rs *RuneStack) Push(r rune) { 12 rs.runes = append(rs.runes, r) 13 rs.dirty = true 14 } 15 16 func (rs *RuneStack) Pop() (bool, rune) { 17 if len(rs.runes) == 0 { 18 return false, '\x00' 19 } 20 21 lastRune := rs.runes[len(rs.runes)-1] 22 rs.runes = rs.runes[0 : len(rs.runes)-1] 23 rs.dirty = true 24 return true, lastRune 25 } 26 27 func (rs *RuneStack) Len() int { 28 return len(rs.runes) 29 } 30 31 func (rs *RuneStack) String() string { 32 if rs.dirty { 33 rs.cached = string(rs.runes) 34 rs.dirty = false 35 } 36 return rs.cached 37 }