LTrim retains a range of elements in a list associated with a key. The range is inclusive, including both the start and stop indices. If the key does not exist, an error is returned. If the list is empty, an error is returned. Negative indices can be used, where -1 represents the last element of the
(key string, start int, stop int)
| 361 | // Negative indices can be used, where -1 represents the last element of the list, |
| 362 | // -2 represents the second last element, and so on. |
| 363 | func (l *ListStructure) LTrim(key string, start int, stop int) error { |
| 364 | // Get the list |
| 365 | lst, _, err := l.getListFromDB(key, false) |
| 366 | if err != nil { |
| 367 | return err |
| 368 | } |
| 369 | if lst.Length == 0 { |
| 370 | return ErrListEmpty |
| 371 | } |
| 372 | |
| 373 | // Calculate the correct indices |
| 374 | start = (start%lst.Length + lst.Length) % lst.Length |
| 375 | stop = (stop%lst.Length + lst.Length) % lst.Length |
| 376 | |
| 377 | if start > stop { |
| 378 | lst = &list{ |
| 379 | Head: nil, |
| 380 | Length: 0, |
| 381 | } |
| 382 | } else { |
| 383 | // Find the new head |
| 384 | newHead := lst.Head |
| 385 | for i := 0; i < start; i++ { |
| 386 | newHead = newHead.Next |
| 387 | } |
| 388 | |
| 389 | // Find the new tail |
| 390 | newTail := newHead |
| 391 | for i := start; i < stop; i++ { |
| 392 | newTail = newTail.Next |
| 393 | } |
| 394 | |
| 395 | // Disconnect the new tail from the rest of the list |
| 396 | newTail.Next = nil |
| 397 | |
| 398 | // Update the list |
| 399 | lst.Head = newHead |
| 400 | lst.Length = stop - start + 1 |
| 401 | } |
| 402 | |
| 403 | // Store in the database |
| 404 | return l.setListToDB(key, lst, 0) |
| 405 | } |
| 406 | |
| 407 | // LIndex returns the value of an element in a list associated with a key based on the index. |
| 408 | // If the key does not exist, an error is returned. |
nothing calls this directly
no test coverage detected