github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/ps/stack.go (about) 1 /* 2 * This file is subject to the terms and conditions defined in 3 * file 'LICENSE.md', which is part of this source code package. 4 */ 5 6 package ps 7 8 type PSStack []PSObject 9 10 func NewPSStack() *PSStack { 11 return &PSStack{} 12 } 13 14 func (stack *PSStack) Empty() { 15 *stack = []PSObject{} 16 } 17 18 func (stack *PSStack) Push(obj PSObject) error { 19 if len(*stack) > 100 { 20 return ErrStackOverflow 21 } 22 23 *stack = append(*stack, obj) 24 return nil 25 } 26 27 func (stack *PSStack) Pop() (PSObject, error) { 28 if len(*stack) < 1 { 29 return nil, ErrStackUnderflow 30 } 31 32 obj := (*stack)[len(*stack)-1] 33 *stack = (*stack)[0 : len(*stack)-1] 34 35 return obj, nil 36 } 37 38 func (stack *PSStack) PopInteger() (int, error) { 39 obj, err := stack.Pop() 40 if err != nil { 41 return 0, err 42 } 43 44 if number, is := obj.(*PSInteger); is { 45 return number.Val, nil 46 } else { 47 return 0, ErrTypeCheck 48 } 49 } 50 51 // Pop and return the numeric value of the top of the stack as a float64. 52 // Real or integer only. 53 func (stack *PSStack) PopNumberAsFloat64() (float64, error) { 54 obj, err := stack.Pop() 55 if err != nil { 56 return 0, err 57 } 58 59 if number, is := obj.(*PSReal); is { 60 return number.Val, nil 61 } else if number, is := obj.(*PSInteger); is { 62 return float64(number.Val), nil 63 } else { 64 return 0, ErrTypeCheck 65 } 66 } 67 68 func (this *PSStack) String() string { 69 s := "[ " 70 for _, obj := range *this { 71 s += obj.String() 72 s += " " 73 } 74 s += "]" 75 76 return s 77 } 78 79 func (this *PSStack) DebugString() string { 80 s := "[ " 81 for _, obj := range *this { 82 s += obj.DebugString() 83 s += " " 84 } 85 s += "]" 86 87 return s 88 }