Make `stringifyIni` overloads public

This commit is contained in:
Elias Batek 2025-02-16 00:43:19 +01:00
parent 6f59ff160c
commit c300956cf7
1 changed files with 63 additions and 3 deletions

66
ini.d
View File

@ -2683,17 +2683,43 @@ private void stringifyIniString(string, OutputRange)(string data, OutputRange ou
} }
} }
private void stringifyIni(StringKey, StringValue, OutputRange)(StringKey key, StringValue value, OutputRange output) { /++
Serializes a `key` + `value` pair to a string in INI format.
+/
void stringifyIni(StringKey, StringValue, OutputRange)(StringKey key, StringValue value, OutputRange output)
if (isCompatibleString!StringKey && isCompatibleString!StringValue) {
stringifyIniString(key, output); stringifyIniString(key, output);
output.put(" = "); output.put(" = ");
stringifyIniString(value, output); stringifyIniString(value, output);
output.put("\n"); output.put("\n");
} }
private void stringifyIni(string, OutputRange)(const IniKeyValuePair!string kvp, OutputRange output) { /// ditto
string stringifyIni(StringKey, StringValue)(StringKey key, StringValue value)
if (isCompatibleString!StringKey && isCompatibleString!StringValue) {
import std.array : appender;
auto output = appender!string();
stringifyIni(key, value, output);
return output[];
}
/++
Serializes an [IniKeyValuePair] to a string in INI format.
+/
void stringifyIni(string, OutputRange)(const IniKeyValuePair!string kvp, OutputRange output) {
return stringifyIni(kvp.key, kvp.value, output); return stringifyIni(kvp.key, kvp.value, output);
} }
/// ditto
string stringifyIni(string)(const IniKeyValuePair!string kvp) {
import std.array : appender;
auto output = appender!string();
stringifyIni(kvp, output);
return output[];
}
private void stringifyIniSectionHeader(string, OutputRange)(string sectionName, OutputRange output) { private void stringifyIniSectionHeader(string, OutputRange)(string sectionName, OutputRange output) {
if (sectionName !is null) { if (sectionName !is null) {
output.put("["); output.put("[");
@ -2702,13 +2728,25 @@ private void stringifyIniSectionHeader(string, OutputRange)(string sectionName,
} }
} }
private void stringifyIni(string, OutputRange)(const IniSection!string section, OutputRange output) { /++
Serializes an [IniSection] to a string in INI format.
+/
void stringifyIni(string, OutputRange)(const IniSection!string section, OutputRange output) {
stringifyIniSectionHeader(section.name, output); stringifyIniSectionHeader(section.name, output);
foreach (const item; section.items) { foreach (const item; section.items) {
stringifyIni(item, output); stringifyIni(item, output);
} }
} }
/// ditto
string stringifyIni(string)(const IniSection!string section) {
import std.array : appender;
auto output = appender!string();
stringifyIni(section, output);
return output[];
}
/++ /++
Serializes an [IniDocument] to a string in INI format. Serializes an [IniDocument] to a string in INI format.
+/ +/
@ -2959,3 +2997,25 @@ string stringifyIni(
assert(ini.indexOf("\nx = \"foo'bar\"\n") > 0); assert(ini.indexOf("\nx = \"foo'bar\"\n") > 0);
assert(ini.indexOf("\n\"\" = bamboozled\n") > 0); assert(ini.indexOf("\n\"\" = bamboozled\n") > 0);
} }
@safe unittest {
const section = IniSection!string("Section Name", [
IniKeyValuePair!string("monkyyy", "business"),
IniKeyValuePair!string("Oachkatzl", "Schwoaf"),
]);
static immutable expected = "[Section Name]\n"
~ "monkyyy = business\n"
~ "Oachkatzl = Schwoaf\n";
assert(stringifyIni(section) == expected);
}
@safe unittest {
const kvp = IniKeyValuePair!string("Key", "Value");
assert(stringifyIni(kvp) == "Key = Value\n");
}
@safe unittest {
assert(stringifyIni("monkyyy", "business lol") == "monkyyy = business lol\n");
}