pretty

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 2 Imported by: 0

README

gore-pretty

gore-pretty (go regular expression prettifier) is a minimal golang module (with no external dependencies) for supporting extended, multi-line regular expressions.

Purpose

Make regular expressions easier to read and understand by allowing them to be written over multiple lines, with additional whitespace and comments.

This is similar to perl's /x regex flag, in that it removes whitespace, newlines and comments before being passed to the regular expression parser.

The stripped string can then be passed to regexp.MustCompile etc. for compilation.

Example

Here is a regular expression to parse ISO 8601 Durations, using pretty.Strip:

var re = regexp.MustCompile(pretty.Strip(`

            # ISO 8601 durations MUST begin with a 'P' (period)
            p

            # calendar part
            # durations must appear in order: years, months, weeks, days
            (?<years>  \d+y )?
            (?<months> \d+m )?
            (?<weeks>  \d+w )?
            (?<days>   \d+d )?

            (?:
                # time part (optional)
                # time parts, if present, MUST be preceeded by a single 'T'
                t

                # times must appear in order: hours, minutes, seconds
                (?<hours>   \d+h )?
                (?<minutes> \d+m )? # note: p1m = 1 month, pt1m = 1 minute
                (?<seconds> \d+s )?
            )?
        `))

and here is the same regular expression as required by regexp.MustCompile():

re := regexp.MustCompile(`p(?<years>\d+y)?(?<months>\d+m)?(?<weeks>\d+w)?(?<days>\d+d)?(?:t(?<hours>\d+h)?(?<minutes>\d+m)?(?<seconds>\d+s)?)?`))

Installation

go get codeberg.org/japh/gore-pretty@latest

Usage

pretty.Strip(string) string is a simple string filter

It takes a string as input and returns a copy of the string after removing all whitespace and comments.

import "codeberg.org/japh/gore-pretty"

var re = regexp.MustCompile(pretty.Strip(`...`))

Filter Rules

  • remove all of the following characters from the input string, unless they are directly preceeded by a backslash (\, 0x5c)
    name code
    tab 0x09
    newline 0x0a
    carriage return 0x0d
    space` 0x20
  • remove unescaped hashes (#) and all text up-to the next newline
  • backslashes which are not immediately followed by a tab, space, newline or hash are left unchanged.

That's it.

If you want to include a space or hash in your pattern, simply preceed it with a backslash. e.g. var re = regexp.MustCompile(pretty.Strip(`a\ b`)) would exactly match the string "a b".

2026 Stephen Riehm [email protected]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Strip

func Strip(input string) string

Strip returns a copy of its input after removing all whitespace and comments.

Whitespace characters are: - tabs (0x09) - newlines (0x0a) - carriage returns (0x0d) - spaces (0x20)

Comments begin with a hash ('#') and continue to the next newline character.

Backslashes may be used to prevent the removal of individual whitespace characters or comments (i.e. the backslash will be removed and the following whitespace character or comment will be kept).

Backslashes before characters not mentioned above have no special meaning and will be kept in the resulting string.

Types

type Scanner

type Scanner struct {
	Input string // input to be scanned
	// contains filtered or unexported fields
}

func (*Scanner) Consume

func (s *Scanner) Consume() string

Consume returns the string of characters that were scanned since the previous call to Consume (or the start of the input string)

func (*Scanner) Next

func (s *Scanner) Next() rune

Next returns the character at the current scan position.

Next does NOT modify the scan position.

func (*Scanner) Peek

func (s *Scanner) Peek() (rune, bool)

Peek returns the next rune from the input start, after the rune at the current scan position.

Peek does NOT modify the scan position.

func (*Scanner) Scan

func (s *Scanner) Scan() bool

Scan moves the scan position to the next rune in the input string.

Scan must be called once before calling [Next] or [Peek]

[Next] and [Peek] should only be called after Scan returns true.

Once scan returns false, it will always return false.

func (*Scanner) SkipChar

func (s *Scanner) SkipChar()

SkipChar moves the scan position and the start of the next consumable span to the next character in the input string

func (*Scanner) SkipSame

func (s *Scanner) SkipSame(r rune)

SkipSame moves the scan position and the start of the next consumable span to the first character which is not the same as the one provided.

func (*Scanner) SkipUntil

func (s *Scanner) SkipUntil(r rune)

SkipUntil moves the scan position and start of the next consumable span to the next occurrance of the provided character.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL