package scalaz
sealed trait StringW extends PimpedType[String] {
lazy val s: String = value
def plural(n: Long): String = if(n == 1L) s else
if((s endsWith "y") && (List("ay", "ey","iy", "oy", "uy") forall (!s.endsWith(_)))) s.take(s.length - 1) + "ies"
else s + "s"
import xml._
import Scalaz._
def node[A: Show](prefix: String, attributes: MetaData, scope: NamespaceBinding, a: Option[A]): Node =
a match {
case Some(t) => Elem(prefix, s, Null, TopScope, t.text)
case None => Text("")
}
def node[A: Show](prefix: String, a: Option[A]): Node =
node(prefix, Null, TopScope, a)
def |:|[A: Show](a: Option[A]): Node =
node(null, a)
def encode(implicit c: CharSet): Array[Byte] = s getBytes c.value
def charsNel : Option[NonEmptyList[Char]] = s.toList.toNel
def charsNel(e: => NonEmptyList[Char]) : NonEmptyList[Char] = this.charsNel getOrElse e
def charsNelErr(message: => String): NonEmptyList[Char] = charsNel(sys.error(message))
def unsafeCharsNel : NonEmptyList[Char] = charsNelErr("cannot turn empty string into NonEmptyList")
import java.io.FileInputStream
def readFile[X](x: X, f: (X, Byte) => X): X = {
val in = new FileInputStream(s)
try {
in.elements.foldLeft(x)(f)
} finally {
in.close
}
}
def fileEach(f: Byte => Unit) {
val in = new FileInputStream(s)
try {
in.elements.foreach(f)
} finally {
in.close
}
}
def readLines[X, Y](x: X, f: (X, Char) => X, y: Y, g: (Y, X) => Y) {
val in = new FileInputStream(s)
try {
var t = x
var u = y
val i = in.elements
val lineSeparators = List('\r'.toByte, '\n'.toByte)
while(i.hasNext) {
val c = i.next.toChar
if(lineSeparators.contains(c)) {
u = g(u, t)
t = x
} else
t = f(t, c)
}
u
} finally {
in.close
}
}
def parseBoolean: Validation[NumberFormatException, Boolean] = try {
success(s.toBoolean)
} catch {
case e: NumberFormatException => failure(e)
}
def parseByte: Validation[NumberFormatException, Byte] = try {
success(java.lang.Byte.parseByte(s))
} catch {
case e: NumberFormatException => failure(e)
}
def parseShort: Validation[NumberFormatException, Short] = try {
success(java.lang.Short.parseShort(s))
} catch {
case e: NumberFormatException => failure(e)
}
def parseInt: Validation[NumberFormatException, Int] = try {
success(java.lang.Integer.parseInt(s))
} catch {
case e: NumberFormatException => failure(e)
}
def parseLong: Validation[NumberFormatException, Long] = try {
success(java.lang.Long.parseLong(s))
} catch {
case e: NumberFormatException => failure(e)
}
def parseFloat: Validation[NumberFormatException, Float] = try {
success(java.lang.Float.parseFloat(s))
} catch {
case e: NumberFormatException => failure(e)
}
def parseDouble: Validation[NumberFormatException, Double] = try {
success(java.lang.Double.parseDouble(s))
} catch {
case e: NumberFormatException => failure(e)
}
}
trait Strings {
implicit def StringTo(ss: String): StringW = new StringW {
val value = ss
}
}