MultilineStrings.jl

Tooling for manipulating multiline strings
Author JuliaStrings
Popularity
4 Stars
Updated Last
1 Year Ago
Started In
August 2020

MultilineStrings

Stable Dev CI Code Style: Blue ColPrac: Contributor's Guide on Collaborative Practices for Community Packages

Tooling for manipulating multiline strings.

Features

The package features support for:

  • Multiline string literals (@m_str, multiline)
  • An indent function which only indents non-blank lines (indent)

Multiline String Literal

The multiline string literal (@m_str), inspired from YAML's block scalars, which provide options for manipulating multiline string literals via a style and chomp indicator:

  • Style indicator:
    • f replace newlines with spaces (folded)
    • l keep newlines (literal)
  • Chomp indicator:
    • s no newlines at the end (strip)
    • c single newline at the end (clip)
    • k keep all newlines from the end (keep)

The indicators are provided after the ending quote of the string (e.g. m"hello\nworld!"fc). If no indicators are provided the default behaviour is folded/strip.

Example

When writing a long string literal you may want to break the string up over multiple lines in the code, to make it easier to read, but have the string be printed as a single line. Specifically, when writing an long error message you may want to break up the string over multiple lines:

"An unexpected error has occurred which really shouldn't have happened but somehow did. Please check that the inputs to this function doesn't contain magic that may interfere with with the non-magical computation occurring on this device."

Alternatively written over multiple lines:

"An unexpected error has occurred which really shouldn't " *
"have happened but somehow did. Please check that the inputs " *
"to this function doesn't contain magic that may interfere with " *
"with the non-magical computation occurring on this device."

Writing strings this way can be cumbersome as you need to remember to add spaces between each line. The MultilineStrings package provides an alternative way of writing this using the multiline string macro:

m"""
An unexpected error has occurred which really shouldn't
have happened but somehow did. Please check that the inputs
to this function doesn't contain magic that may interfere with
with the non-magical computation occurring on this device.
"""

Take note that a Julia triple-quoted string literal will leave most newlines in place.

Indent

The indent function will indent non-empty lines of a string by a number of spaces.

julia> str = """
           A blank line:

           plus another line at the end.
           """
"A blank line:\n\nplus another line at the end.\n"

julia> indent(str, 4)
"    A blank line:\n\n    plus another line at the end.\n"

Required Packages

No packages found.