strings.go 609 B

12345678910111213141516171819202122232425
  1. package rulesfn
  2. // Substring returns the substring of the input provided. If the start or stop
  3. // indexes are not valid for the input nil will be returned. If errors occur
  4. // they will be added to the provided [ErrorCollector].
  5. func SubString(input string, start, stop int, reverse bool) *string {
  6. if start < 0 || stop < 1 || start >= stop || len(input) < stop {
  7. return nil
  8. }
  9. for _, r := range input {
  10. if r > 127 {
  11. return nil
  12. }
  13. }
  14. if !reverse {
  15. v := input[start:stop]
  16. return &v
  17. }
  18. rStart := len(input) - stop
  19. rStop := len(input) - start
  20. return SubString(input, rStart, rStop, false)
  21. }