StringParser

info.fingo.spata.text.StringParser
See theStringParser companion trait
object StringParser

Parsing methods from String to various simple types.

Contains parsers for common types, like numbers, dates and times.

Additional parsers may be provided by implementing StringParser or FormattedStringParser traits and putting given instances in scope. StringParser may be implemented if there are no different formatting options for given type, e.g. for integers (although one may argue if this is a good example). For all cases when different formatting options exist, FormattedStringParser should be implemented.

Attributes

Companion
trait
Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Type members

Classlikes

final class Pattern[A]

Intermediary to delegate parsing to in order to infer type of formatter used by parser.

Intermediary to delegate parsing to in order to infer type of formatter used by parser.

When adding parsing function to an entity, instead of providing

def parse[A, B](input: String, format: B)(using parser: FormattedStringParser[A, B]): ParseResult[A]

which requires then to use it as

entity.parse[Double, NumberFormat]("123,45", numberFormat)

one can provide function to get this Pattern

def parse[A]: Pattern[A]

and use it with nicer syntax

entity.parse[Double]("123,45", numberFormat)

Type parameters

A

target type for parsing

Attributes

Supertypes
class Object
trait Matchable
class Any
object bigDecimalParserFmt extends FormattedStringParser[BigDecimal, DecimalFormat]

Parser for decimal values with support for formats.

Parser for decimal values with support for formats.

Attributes

Supertypes
trait FormattedStringParser[BigDecimal, DecimalFormat]
class Object
trait Matchable
class Any
Self type

Parser for boolean values with support for formats.

Parser for boolean values with support for formats.

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type
object doubleParserFmt extends FormattedStringParser[Double, DecimalFormat]

Parser for double values with support for formats.

Parser for double values with support for formats.

Attributes

Supertypes
trait FormattedStringParser[Double, DecimalFormat]
class Object
trait Matchable
class Any
Self type
object intParser extends StringParser[Int]

Parser for integer values.

Parser for integer values.

Attributes

Supertypes
trait StringParser[Int]
class Object
trait Matchable
class Any
Self type
intParser.type
object localDateParserFmt extends FormattedStringParser[LocalDate, DateTimeFormatter]

Parser for date values with support for formats.

Parser for date values with support for formats.

Attributes

Supertypes
trait FormattedStringParser[LocalDate, DateTimeFormatter]
trait StringParser[LocalDate]
class Object
trait Matchable
class Any
Self type
object localDateTimeParserFmt extends FormattedStringParser[LocalDateTime, DateTimeFormatter]

Parser for date with time values with support for formats.

Parser for date with time values with support for formats.

Attributes

Supertypes
trait FormattedStringParser[LocalDateTime, DateTimeFormatter]
trait StringParser[LocalDateTime]
class Object
trait Matchable
class Any
Self type
object localTimeParserFmt extends FormattedStringParser[LocalTime, DateTimeFormatter]

Parser for time values with support for formats.

Parser for time values with support for formats.

Attributes

Supertypes
trait FormattedStringParser[LocalTime, DateTimeFormatter]
trait StringParser[LocalTime]
class Object
trait Matchable
class Any
Self type
object longParserFmt extends FormattedStringParser[Long, NumberFormat]

Parser for long values with support for formats.

Parser for long values with support for formats.

Attributes

Supertypes
trait FormattedStringParser[Long, NumberFormat]
class Object
trait Matchable
class Any
Self type
object stringParser extends StringParser[String]

No-op parser for strings.

No-op parser for strings.

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type

Value members

Concrete methods

def parse[A](str: String)(using parser: StringParser[A]): ParseResult[A]

Safely parses string to desired type.

Safely parses string to desired type.

Type parameters

A

target type for parsing

Value parameters

parser

the parser for specific target type

str

the input string

Attributes

Returns

either parsed value or an ParseError

Example
import info.fingo.spata.text.StringParser.*
val x = parse[Double]("123.45").getOrElse(Double.NaN)
val y = parse[Option[Double]]("123.45").map(_.getOrElse(0.0)).getOrElse(Double.NaN)
def parse[A]: Pattern[A]

Parses string to desired type based on provided format.

Parses string to desired type based on provided format.

Delegates actual parsing to Pattern#apply method.

Type parameters

A

target type for parsing

Attributes

Returns

intermediary to retrieve value according to custom format

See also

parse without Pattern for sample usage.

Givens

Givens

Parser for decimal values with support for formats.

Parser for decimal values with support for formats.

Attributes

Parser for boolean values with support for formats.

Parser for boolean values with support for formats.

Attributes

Parser for double values with support for formats.

Parser for double values with support for formats.

Attributes

given intParser: intParser.type

Parser for integer values.

Parser for integer values.

Attributes

Parser for date values with support for formats.

Parser for date values with support for formats.

Attributes

Parser for date with time values with support for formats.

Parser for date with time values with support for formats.

Attributes

Parser for time values with support for formats.

Parser for time values with support for formats.

Attributes

Parser for long values with support for formats.

Parser for long values with support for formats.

Attributes

given optionParser[A](using parser: StringParser[A]): optionParser[A]

Parser for optional values. Allows conversion of any simple parser to return Option[A] instead of A, avoiding error for empty string.

Parser for optional values. Allows conversion of any simple parser to return Option[A] instead of A, avoiding error for empty string.

Type parameters

A

the simple type wrapped by scala.Option

Value parameters

parser

the parser for underlying simple type

Attributes

Returns

parser which accepts empty input

given optionParserFmt[A, B](using parser: FormattedStringParser[A, B]): optionParserFmt[A, B]

Parser for optional values with support for different formats. Allows conversion of any simple parser to return Option[A] instead of A, avoiding error for empty string.

Parser for optional values with support for different formats. Allows conversion of any simple parser to return Option[A] instead of A, avoiding error for empty string.

Type parameters

A

the simple type wrapped by scala.Option

B

type of formatter

Value parameters

parser

the parser for underlying simple type

Attributes

Returns

parser which support formatted input and accepts empty one

No-op parser for strings.

No-op parser for strings.

Attributes