Merge branch 'main' of https://git.neemek.com/neemek/anglais
All checks were successful
/ test (push) Successful in 53s
All checks were successful
/ test (push) Successful in 53s
This commit is contained in:
commit
10f55313b0
2 changed files with 26 additions and 9 deletions
|
|
@ -17,17 +17,22 @@ func NewStack[T any](capacity Pos) *Stack[T] {
|
||||||
|
|
||||||
func (s *Stack[T]) Push(items ...T) {
|
func (s *Stack[T]) Push(items ...T) {
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
|
s.pushItem(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stack[T]) pushItem(item T) {
|
||||||
if s.Current >= s.Capacity {
|
if s.Current >= s.Capacity {
|
||||||
panic("stack overflow")
|
panic("stack overflow")
|
||||||
}
|
}
|
||||||
if int(s.Current) == len(s.items) {
|
if int(s.Current) == len(s.items) {
|
||||||
s.items = append(s.items, item)
|
s.items = append(s.items, item)
|
||||||
|
} else {
|
||||||
|
s.items[s.Current] = item
|
||||||
}
|
}
|
||||||
|
|
||||||
s.items[s.Current] = item
|
|
||||||
s.Current++
|
s.Current++
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stack[T]) Pop() T {
|
func (s *Stack[T]) Pop() T {
|
||||||
if s.Current <= 0 {
|
if s.Current <= 0 {
|
||||||
|
|
|
||||||
12
tests/strings.ang
Normal file
12
tests/strings.ang
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
|
||||||
|
txt := "Hello world!"
|
||||||
|
|
||||||
|
# string length
|
||||||
|
assertEq(txt.length(), 12)
|
||||||
|
|
||||||
|
# string split
|
||||||
|
assertEq(txt.split(" ").length(), 2)
|
||||||
|
|
||||||
|
# string indexing
|
||||||
|
assertEq(txt.at(1), "e")
|
||||||
|
assertEq(txt.at(6), "w")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue