Class ValueParsing
Namespace: ValveResourceFormat.Utils
Assembly: ValveResourceFormat.dll
Converts text to numbers, booleans, vectors and colors with the rules the engine applies to keyvalue strings. The Parse and TryParse methods need the whole text to be a value, the ParseLeading methods read the number it starts with. Failures never throw: the Parse methods return a default and the TryParse methods return false.
public static class ValueParsingInheritance
object ← ValueParsing
Methods
ParseAxis(ReadOnlySpan<char>, Span<float>)
Reads the two points of an axis, written as "x y z, x y z", into six components. The numbers are read like ValueParsing.ParseFloatArray(ReadOnlySpan<char>, Span<float>), and the end point is read only when the start point has three numbers and a comma follows it.
public static int ParseAxis(ReadOnlySpan<char> text, Span<float> destination)Parameters
text ReadOnlySpan<char>
The text to parse.
destination Span<float>
Receives the start point and then the end point, six components; the ones not read are zero.
Returns
int
How many numbers were read.
ParseBool(ReadOnlySpan<char>, bool)
Parses a boolean: "true", "1" and "yes" are true, "false", "0" and "no" are false, in any case. Any other text that is wholly a base-10 integer or a float is true unless it is zero, and empty text is false.
public static bool ParseBool(ReadOnlySpan<char> text, bool defaultValue = false)Parameters
text ReadOnlySpan<char>
The text to parse.
defaultValue bool
The value returned for any other text.
Returns
bool
The parsed value, or defaultValue.
Remarks
Trailing text fails, unlike the leading-number methods, so "1abc" gives defaultValue.
ParseFloat(ReadOnlySpan<char>, float)
Parses a float like ValueParsing.TryParseFloat(ReadOnlySpan<char>, out float).
public static float ParseFloat(ReadOnlySpan<char> text, float defaultValue = 0)Parameters
text ReadOnlySpan<char>
The text to parse.
defaultValue float
The value returned when the text is not a float.
Returns
float
The parsed value, or defaultValue.
ParseFloatArray(ReadOnlySpan<char>, Span<float>)
Reads numbers one after another into destination. White space before each number is skipped and no separator is needed, so "1-2" reads as 1 and -2. Each number is the longest prefix that parses as a float, and reading stops at the first position that is not a number.
public static int ParseFloatArray(ReadOnlySpan<char> text, Span<float> destination)Parameters
text ReadOnlySpan<char>
The text to parse.
destination Span<float>
Receives the numbers; components past the count read are zero.
Returns
int
How many numbers were read.
Remarks
For values the engine reads as numbers one after another, such as angles and particle control points.
ParseLeadingFloat(ReadOnlySpan<char>)
Reads the number text starts with the way C's atof does: an optional sign, digits, a fraction after a point, and an exponent.
public static float ParseLeadingFloat(ReadOnlySpan<char> text)Parameters
text ReadOnlySpan<char>
The text, which may start with white space.
Returns
float
The number, or zero when the text starts with none.
Remarks
Real data never contains the cases where this differs from the engine: "inf" and "nan" are read where the engine reads 0, and a second decimal point ends the number where the engine skips the earlier ones, so "1.2.3" is 1.2 and not 12.3.
ParseLeadingFloats(ReadOnlySpan<char>, ReadOnlySpan<char>, Span<float>)
Reads the leading number of each token of text, where tokens are separated by any of the separator characters and empty tokens are skipped.
public static int ParseLeadingFloats(ReadOnlySpan<char> text, ReadOnlySpan<char> separators, Span<float> destination)Parameters
text ReadOnlySpan<char>
The text to split.
separators ReadOnlySpan<char>
The characters that separate tokens.
destination Span<float>
Receives the numbers in order. Entries past the last token are zero.
Returns
int
How many tokens were read, at most the length of destination.
Remarks
Each token is read with ValueParsing.ParseLeadingFloat(ReadOnlySpan<char>), so a token without a number is zero.
ParseLeadingInt(ReadOnlySpan<char>)
Reads the integer text starts with like ValueParsing.TryParseLeadingInt: an optional sign and the digits after it.
public static int ParseLeadingInt(ReadOnlySpan<char> text)Parameters
text ReadOnlySpan<char>
The text, which may start with white space.
Returns
int
The integer, or zero when the text starts with none or it does not fit.
ParseSoundEventBool(ReadOnlySpan<char>)
Parses a sound event boolean: "true" and "1" in any case, or any text containing "1.0", are true. All other text is false.
public static bool ParseSoundEventBool(ReadOnlySpan<char> text)Parameters
text ReadOnlySpan<char>
The text to parse.
Returns
bool
The parsed value.
Remarks
"1.0" matches anywhere in the text, so "11.05" is true.
ParseVector(ReadOnlySpan<char>, Span<float>)
Parses space separated floats into destination. Empty parts are dropped, a part that is not a float reads as zero, missing components are zero and extra parts are ignored.
public static int ParseVector(ReadOnlySpan<char> text, Span<float> destination)Parameters
text ReadOnlySpan<char>
The text to parse.
destination Span<float>
Receives the components.
Returns
int
How many parts were read, at most the length of destination.
Remarks
Zero-filling is the rule for keyfields. Some other engine readers keep the whole default when the count is wrong.
TryParseColor(ReadOnlySpan<char>, out Color32, bool)
Parses a space separated color. Four parts are red, green, blue and alpha, and three are red, green and blue with full alpha. Each part is read with ValueParsing.ParseLeadingInt(ReadOnlySpan<char>), so "0.5" is 0, and wraps to a byte, so 256 is 0 and -1 is 255.
public static bool TryParseColor(ReadOnlySpan<char> text, out Color32 color, bool clamp = false)Parameters
text ReadOnlySpan<char>
The text to parse.
color Color32
The color, or the default color when the text has any other number of parts.
clamp bool
Whether parts are clamped to 0-255 instead of wrapping.
Returns
bool
Whether the text has three or four parts.
Remarks
Parts wrap to a byte as the engine's color storage does. Other part counts are left to the caller, whose default differs per key.
TryParseDouble(ReadOnlySpan<char>, out double)
Parses a whole string as a double with the same rules as ValueParsing.TryParseFloat(ReadOnlySpan<char>, out float).
public static bool TryParseDouble(ReadOnlySpan<char> text, out double value)Parameters
text ReadOnlySpan<char>
The text to parse.
value double
The parsed value, or zero on failure.
Returns
bool
Whether the whole text was a number.
TryParseFloat(ReadOnlySpan<char>, out float)
Parses a whole string as a float. Surrounding white space and one leading plus sign are skipped; the rest must be a number, or an infinity or NaN spelled "inf", "infinity" or "nan" in any case. Other trailing characters, thousands separators and hex are failures. A value out of range is infinity or zero.
public static bool TryParseFloat(ReadOnlySpan<char> text, out float value)Parameters
text ReadOnlySpan<char>
The text to parse.
value float
The parsed value, or zero on failure.
Returns
bool
Whether the whole text was a float.
Remarks
Trailing text fails, unlike the leading-number methods.
TryParseInt(ReadOnlySpan<char>, out int)
Parses a whole string as a 32-bit integer: optional leading white space, an optional sign and base-10 digits. A fraction, trailing characters and values outside the int range are failures.
public static bool TryParseInt(ReadOnlySpan<char> text, out int value)Parameters
text ReadOnlySpan<char>
The text to parse.
value int
The parsed value, or zero on failure.
Returns
bool
Whether the whole text was an integer.
TryParseLeadingLong(ReadOnlySpan<char>, out long)
Reads the 64-bit integer text starts with, with the same rules as ValueParsing.TryParseLeadingInt.
public static bool TryParseLeadingLong(ReadOnlySpan<char> text, out long value)Parameters
text ReadOnlySpan<char>
The text to parse.
value long
The integer, or zero when there are no digits or it does not fit.
Returns
bool
Whether the text starts with an integer.
Remarks
An overflow fails where the engine clamps to the 64-bit range.
TryParseVector3(ReadOnlySpan<char>, out Vector3)
Reads exactly three numbers like ValueParsing.ParseFloatArray(ReadOnlySpan<char>, Span<float>).
public static bool TryParseVector3(ReadOnlySpan<char> text, out Vector3 value)Parameters
text ReadOnlySpan<char>
The text to parse.
value Vector3
The vector, or zero when the text does not hold three numbers.
Returns
bool
Whether three numbers were read.

