String Parsers (string, lit)

| Home | Libraries | People | FAQ | More

##### String Parsers (`string`, `lit`)

###### Description

The `string` parser matches a string of characters. The `string` parser is an implicit lexeme: the `skip` parser is not applied in between characters of the string. The `string` parser has an associated Character Encoding Namespace. This is needed when doing basic operations such as inhibiting case sensitivity. Examples:

    string("Hello")
    string(L"Hello")
    string(s) // s is a std::string

`lit`, like `string`, also matches a string of characters. The main difference is that `lit` does not synthesize an attribute. A plain string like `"hello"` or a `std::basic_string` is equivalent to a `lit`. Examples:

    "Hello"
    lit("Hello")
    lit(L"Hello")
    lit(s) // s is a std::string

###### Header

    // forwards to <boost/spirit/home/qi/string/lit.hpp>
    #include <boost/spirit/include/qi_lit.hpp>

###### Namespace

Name
`boost::spirit::lit // alias: boost::spirit::qi::lit`
`ns::string`

In the table above, `ns` represents a Character Encoding Namespace.

###### Model of

> `PrimitiveParser`

Notation

`s`

  A String or a Lazy Argument that evaluates to a String.

`ns`

  A Character Encoding Namespace.

###### Expression Semantics

Semantics of an expression is defined only where it differs from, or is not defined in `PrimitiveParser`.

Expression | Semantics
--- | ---
`s` | Create string parser from a string, `s`.
`lit(s)` | Create a string parser from a string, `s`.
`ns::string(s)` | Create a string parser with `ns` encoding from a string, `s`.

###### Attributes

Expression | Attribute
--- | ---
`s` | `unused`
`lit(s)` | `unused`
`ns::string(s)` | `std::basic_string<T>` where `T` is the underlying character type of `s`.

###### Complexity

> O(N)

where `N` is the number of characters in the string to be parsed.

###### Example

| Note
The test harness for the example(s) below is presented in the Basics Examples section.

Some using declarations:

    using boost::spirit::qi::lit;
    using boost::spirit::ascii::string;

Basic literals:

    test_parser("boost", "boost");          // plain literal
    test_parser("boost", lit("boost"));     // explicit literal
    test_parser("boost", string("boost"));  // ascii::string

From a `std::string`

    std::string s("boost");
    test_parser("boost", s);            // direct
    test_parser("boost", lit(s));       // explicit
    test_parser("boost", string(s));    // ascii::string

Lazy strings using Phoenix

    namespace phx = boost::phoenix;
    test_parser("boost", phx::val("boost"));            // direct
    test_parser("boost", lit(phx::val("boost")));       // explicit
    test_parser("boost", string(phx::val("boost")));    // ascii::string