Skip to content

encoding.ini #

encoding.ini

parse_string(text)! and parse_file(path)! read a strict INI dialect. Headers use [section], including an empty section name; entries use key = value or key : value. Blank lines and full-line ; or # comments are ignored. Duplicate sections or keys, keys before a header, missing separators, and unbalanced headers produce errors with line numbers. Values retain inline ; and # characters.

get(section, key) returns an optional string; get_or(section, key, fallback) supplies a default. set(section, key, value) creates a missing section. sections() returns section names in insertion order. encode() writes sections in that order and sorts their keys.

Encoding does not escape names or values. For reparsable output, use single-line names and values, section names without ], and keys without = or : or a leading [, ;, or #. The parser trims surrounding whitespace from names, keys, and values.

fn parse_file #

fn parse_file(path string) !IniFile

parse_file reads and parses the INI file at path.

fn parse_string #

fn parse_string(s string) !IniFile

parse_string parses an INI document. Sections are [name] headers; entries are key = value or key : value. Full-line ;/# comments and blank lines are skipped; keys before any section, duplicate sections/keys and lines without a separator are errors. There are intentionally no inline comments, so values like URLs with fragments survive intact.

struct IniFile #

struct IniFile {
mut:
	sections map[string]map[string]string
	order    []string
}

IniFile is a parsed INI document: ordered sections of key/value pairs. It is the V-idiomatic equivalent of Python's configparser.

fn (IniFile) get #

fn (f &IniFile) get(section string, key string) ?string

get returns the value for section/key, or none if absent.

fn (IniFile) get_or #

fn (f &IniFile) get_or(section string, key string, default string) string

get_or returns the value for section/key, or default if absent.

fn (IniFile) set #

fn (mut f IniFile) set(section string, key string, value string)

set stores value under section/key, creating the section on first use.

fn (IniFile) sections #

fn (f &IniFile) sections() []string

sections returns section names in file order.

fn (IniFile) encode #

fn (f &IniFile) encode() string

encode renders sections in insertion order and keys in sorted order. Names and values must use the single-line syntax accepted by parse_string; surrounding whitespace is trimmed when the encoded text is parsed again.