aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuicommon/utils/StringOps.scala
blob: eaac76125a44b724873417361afd7189c5c39dc1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package xyz.driver.pdsuicommon.utils

import xyz.driver.pdsuicommon.utils.Implicits.toCharOps

final class StringOps(val self: String) extends AnyVal {

  def safeTrim: String = {
    def shouldKeep(c: Char): Boolean = !c.isSafeControl && !c.isSafeWhitespace

    if (self.isEmpty) {
      ""
    } else {
      val start = self.indexWhere(shouldKeep)
      val end   = self.lastIndexWhere(shouldKeep)

      if (start >= 0 && end >= 0) {
        self.substring(start, end + 1)
      } else {
        ""
      }
    }
  }
}