Enhanced C#
Language of your choice: library documentation

Documentation moved to ecsharp.net

GitHub doesn't support HTTP redirects, so you'll be redirected in 3 seconds.

 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
Nested classes | Public Types | Public static fields | Static Public Member Functions | List of all members
Loyc.G Class Reference

Contains global functions that don't belong in any specific class. More...


Source file:

Remarks

Contains global functions that don't belong in any specific class.

Public Types

enum  ParseFlag {
  ParseFlag.SkipSpacesInFront = 1, ParseFlag.SkipSpacesInsideNumber = 2, ParseFlag.StopBeforeOverflow = 4,
  ParseFlag.SkipUnderscores = 8, ParseFlag.AllowCommaDecimalPoint = 8
}
 Flags that can be used with the overload of TryParseAt() that parses unsigned integers. More...
 

Public static fields

static readonly object BoxedFalse = false
 Singleton false cast to object.
 
static readonly object BoxedTrue = true
 Singleton true cast to object.
 
static readonly object BoxedVoid = new @void()
 Singleton void cast to object.
 

Static Public Member Functions

static void Swap< T > (ref T a, ref T b)
 
static bool IsOneOf< T > (this T value, params T[] set)
 
static Comparison< T > ToComparison< T > ()
 Gets a Comparison{T} for the specified type. More...
 
static Func< T, T, int > ToComparisonFunc< T > ()
 
static Comparison< T > ToComparison< T > (IComparer< T > pred)
 Converts an IComparer{T} to a Comparison{T}. More...
 
static Func< T, T, int > ToComparisonFunc< T > (IComparer< T > pred)
 Converts an IComparer{T} to a Func{T,T,int}. More...
 
static List< string > SplitCommandLineArguments (string listString)
 
static string MakeValidFileName (string text, char?replacement= '_', bool fancy=true)
 Replaces characters in text that are not allowed in file names with the specified replacement character. More...
 
static Pair< T1, T2 > Pair< T1, T2 > (T1 a, T2 b)
 
static Triplet< T1, T2, T3 > Triplet< T1, T2, T3 > (T1 a, T2 b, T3 c)
 
static bool Verify (bool condition)
 Same as Debug.Assert except that the argument is evaluated even in a Release build. More...
 
static void Require (bool condition)
 
static void Require (bool condition, string msg)
 
static bool TryParseHex (UString s, out int value)
 
static int TryParseHex (ref UString s, out int value)
 
static int HexDigitValue (char c)
 Gets the integer value for the specified hex digit, or -1 if the character is not a hex digit. More...
 
static int Base36DigitValue (char c)
 
static char HexDigitChar (int value)
 Gets the hex digit character for the specified value, or '?' if the value is not in the range 0...15. More...
 
static string EscapeCStyle (UString s, EscapeC flags=EscapeC.Default)
 
static string EscapeCStyle (UString s, EscapeC flags, char quoteType)
 
static bool EscapeCStyle (char c, StringBuilder @out, EscapeC flags=EscapeC.Default, char quoteType= '\0')
 
static string UnescapeCStyle (string s)
 Unescapes a string that uses C-style escape sequences, e.g. "\n\r" becomes "
". More...
 
static string UnescapeCStyle (string s, int index, int length, bool removeUnnecessaryBackslashes)
 Unescapes a string that uses C-style escape sequences, e.g. "\n\r" becomes "
". More...
 
static char UnescapeChar (ref UString s)
 
static char UnescapeChar (string s, ref int i)
 Unescapes a single character of a string, e.g. int = 3; UnescapeChar("foo\\n", ref i) == '
'
. Returns the character at 'index' if it is not a backslash, or if it is a backslash but no escape sequence could be discerned. More...
 
static PushedTLV< T > PushTLV< T > (ThreadLocalVariable< T > variable, T newValue)
 Helper function for a using statement that temporarily modifies a thread-local variable. More...
 
static bool TryParseInt (string s, ref int index, out int result, int radix=10, bool skipSpaces=true)
 Tries to parse a string to an integer. Unlike Int32.TryParse(string, out int), this method allows parsing to start at any point in the string, it allows non-numeric data after the number, and it can parse numbers that are not in base 10. More...
 
static bool TryParseInt (ref UString s, out int result, int radix=10, ParseFlag flags=0)
 
static bool TryParseInt (ref UString input, out long result, int radix=10, ParseFlag flags=0)
 
static bool TryParseUInt (ref UString s, out ulong result, int radix=10, ParseFlag flags=0)
 
static bool TryParseFloatParts (ref UString source, int radix, out bool negative, out ulong mantissa, out int exponentBaseR, out int exponentBase2, out int exponentBase10, out int numDigits, ParseFlag flags=0)
 Low-level method that identifies the parts of a float literal of arbitrary base (typically base 2, 10, or 16) with no prefix or suffix, such as 2.Cp0 (which means 2.75 in base 16). More...
 
static bool TryParseFloatParts (ref UString source, int radix, out bool negative, out ulong mantissa, out int exponentBase2, out int exponentBase10, out int numDigits, ParseFlag flags=0)
 Parses the parts of a floating-point string. See the other overload for details. More...
 
static double TryParseDouble (ref UString source, int radix, ParseFlag flags=0)
 Parses a string to a double-precision float, returning NaN on failure or an infinity value on overflow. More...
 
static float TryParseFloat (ref UString source, int radix, ParseFlag flags=0)
 Parses a string to a single-precision float, returning NaN on failure or an infinity value on overflow. More...
 
static int SkipSpaces (string s, int index)
 Gets the index of the first non-space character after the specified index. More...
 
static UString SkipSpaces (UString s)
 Returns a string with any spaces and tabs removed from the beginning. More...
 
static string BareHtmlEntityNameForAscii (char c)
 Gets a bare HTML entity name for an ASCII character, or null if there is no entity name for the given character, e.g. '"'=>"quot". More...
 

Member Enumeration Documentation

Flags that can be used with the overload of TryParseAt() that parses unsigned integers.

Enumerator
SkipSpacesInFront 

Skip spaces before the number. Without this flag, spaces make parsing fail.

SkipSpacesInsideNumber 

Skip spaces inside the number. Without this flag, spaces make parsing stop.

StopBeforeOverflow 

Changes overflow handling behavior when parsing an integer, so that the result does not overflow (wrap), and the digit(s) at the end of the string, that would have caused overflow, are ignored. In this case, the return value is still false.

SkipUnderscores 

Skip underscores inside number. Without this flag, underscores make parsing stop.

AllowCommaDecimalPoint 

Whether to treat comma as a decimal point when parsing a float. The dot '.' is always treated as a decimal point.

Member Function Documentation

static string Loyc.G.BareHtmlEntityNameForAscii ( char  c)
inlinestatic

Gets a bare HTML entity name for an ASCII character, or null if there is no entity name for the given character, e.g. '"'=>"quot".

The complete entity name is "&" + GetHtmlEntityNameForAscii(c) + ";". Some HTML entities have multiple names; this function returns one of them. There is a name in this table for all ASCII punctuation characters.

static char Loyc.G.HexDigitChar ( int  value)
inlinestatic

Gets the hex digit character for the specified value, or '?' if the value is not in the range 0...15.

static int Loyc.G.HexDigitValue ( char  c)
inlinestatic

Gets the integer value for the specified hex digit, or -1 if the character is not a hex digit.

static string Loyc.G.MakeValidFileName ( string  text,
char?  replacement = '_',
bool  fancy = true 
)
inlinestatic

Replaces characters in text that are not allowed in file names with the specified replacement character.

Parameters
textText to make into a valid filename. The same string is returned if it is valid already.
replacementReplacement character, or null to simply remove bad characters.
fancyWhether to replace quotes and slashes with the non-ASCII characters ” and ⁄.
Returns
A string that can be used as a filename. If the output string would otherwise be empty, returns "_".
static PushedTLV<T> Loyc.G.PushTLV< T > ( ThreadLocalVariable< T >  variable,
newValue 
)
inlinestatic

Helper function for a using statement that temporarily modifies a thread-local variable.

Parameters
variableVariable to change
newValueNew value

// Temporarily change the target of compiler error messages using (var _ = G.PushTLV(CompilerOutput.Writer, CustomWriter)) { Warning.Write(SourcePos.Nowhere, "This message will go to a custom writer"); } Warning.Write(SourcePos.Nowhere, "But this message will go to the original one");

static int Loyc.G.SkipSpaces ( string  s,
int  index 
)
inlinestatic

Gets the index of the first non-space character after the specified index.

Only ' ' and '' are treated as spaces. If the index is invalid, it is returned unchanged.

Referenced by Loyc.G.TryParseFloatParts(), and Loyc.G.TryParseInt().

static UString Loyc.G.SkipSpaces ( UString  s)
inlinestatic

Returns a string with any spaces and tabs removed from the beginning.

static Comparison<T> Loyc.G.ToComparison< T > ( )
inlinestatic

Gets a Comparison{T} for the specified type.

This method is optimized and does not allocate on every call.

Type Constraints
T :IComparable<T> 
static Comparison<T> Loyc.G.ToComparison< T > ( IComparer< T >  pred)
inlinestatic

Converts an IComparer{T} to a Comparison{T}.

static Func<T, T, int> Loyc.G.ToComparisonFunc< T > ( IComparer< T >  pred)
inlinestatic

Converts an IComparer{T} to a Func{T,T,int}.

static double Loyc.G.TryParseDouble ( ref UString  source,
int  radix,
ParseFlag  flags = 0 
)
inlinestatic

Parses a string to a double-precision float, returning NaN on failure or an infinity value on overflow.

Parameters
radixBase of the number to parse; must be 2 (binary), 4, 8 (octal), 10 (decimal), 16 (hexadecimal) or 32.
flagsAlters parsing behavior, see ParseFlags.

References Loyc.G.TryParseFloatParts().

static float Loyc.G.TryParseFloat ( ref UString  source,
int  radix,
ParseFlag  flags = 0 
)
inlinestatic

Parses a string to a single-precision float, returning NaN on failure or an infinity value on overflow.

Parameters
radixBase of the number to parse; must be 2 (binary), 4, 8 (octal), 10 (decimal), 16 (hexadecimal) or 32.
flagsAlters parsing behavior, see ParseFlags.

References Loyc.G.TryParseFloatParts().

static bool Loyc.G.TryParseFloatParts ( ref UString  source,
int  radix,
out bool  negative,
out ulong  mantissa,
out int  exponentBaseR,
out int  exponentBase2,
out int  exponentBase10,
out int  numDigits,
ParseFlag  flags = 0 
)
inlinestatic

Low-level method that identifies the parts of a float literal of arbitrary base (typically base 2, 10, or 16) with no prefix or suffix, such as 2.Cp0 (which means 2.75 in base 16).

Parameters
radixBase of the number to parse; must be between 2 and 36.
mantissaInteger magnitude of the number.
exponentBase2Base-2 exponent to apply, as specified by the 'p' suffix, or 0 if there is no 'p' suffix..
exponentBase10Base-10 exponent to apply, as specified by the 'e' suffix, or 0 if there is no 'e' suffix..
exponentBaseRBase-radix exponent to apply. This number is based on the front part of the number only (not including the 'p' or 'e' suffix). Negative values represent digits after the decimal point, while positive numbers represent 64-bit overflow. For example, if the input is 12.3456 with radix=10, the output will be mantissa=123456 and exponentBaseR=-4. If the input is 0123_4567_89AB_CDEF_1234.5678 with radix=16, the mantissa overflows, and the result is mantissa = 0x1234_5678_9ABC_DEF1 with exponentBaseR=3.
numDigitsSet to the number of digits in the number, not including the exponent part.
flagsAlters parsing behavior, see ParseFlags.

The syntax required is

( '+'|'-' )?
( Digits ('.' Digits?)? | '.' Digits )
( ('p'|'P') ('-'|'+')? DecimalDigits+ )?
( ('e'|'E') ('-'|'+')? DecimalDigits+ )?

where Digits refers to one digits in the requested base, possibly including underscores or spaces if the flags allow it; similarly, DecimalDigits refers to base-10 digits and is also affected by the flags.

Returns false if there was an error interpreting the input.

To keep the parser relatively simple, it does not roll back in case of error the way the int parser does. For example, given the input "23p", the 'p' is consumed and causes the method to return false, even though the parse could have been successful if it had ignored the 'p'.

References Loyc.G.SkipSpaces(), and Loyc.G.TryParseInt().

Referenced by Loyc.G.TryParseDouble(), Loyc.G.TryParseFloat(), and Loyc.G.TryParseFloatParts().

static bool Loyc.G.TryParseFloatParts ( ref UString  source,
int  radix,
out bool  negative,
out ulong  mantissa,
out int  exponentBase2,
out int  exponentBase10,
out int  numDigits,
ParseFlag  flags = 0 
)
inlinestatic

Parses the parts of a floating-point string. See the other overload for details.

Parameters
radixBase of the number to parse; must be 2 (binary), 4, 8 (octal), 10 (decimal), 16 (hexadecimal) or 32.
negativetrue if the string began with '-'.
mantissaInteger magnitude of the number.
exponentBase2Base-2 exponent to apply.
exponentBase10Base-10 exponent to apply.
numDigitsSet to the number of digits in the number, not including the exponent part.
flagsAlters parsing behavior, see ParseFlags.

This method is a wrapper around the other overload that combines the 'exponentBaseR' parameter with 'exponentBase2' or 'exponentBase10' depending on the radix. For example, when radix=10, this method adds exponentBaseR to exponentBase10.

References Loyc.G.TryParseFloatParts().

static bool Loyc.G.TryParseInt ( string  s,
ref int  index,
out int  result,
int  radix = 10,
bool  skipSpaces = true 
)
inlinestatic

Tries to parse a string to an integer. Unlike Int32.TryParse(string, out int), this method allows parsing to start at any point in the string, it allows non-numeric data after the number, and it can parse numbers that are not in base 10.

Parameters
radixNumber base, e.g. 10 for decimal and 2 for binary.
indexLocation at which to start parsing
flagsParseFlags that affect parsing behavior.
skipSpacesWhether to skip spaces before parsing. Only the ' ' and '' characters are treated as spaces. No space is allowed between '-' and the digits of a negative number, even with this flag.
Returns
True if a number was found starting at the specified index and it was successfully converted to a number, or false if not.

This method never throws. If parsing fails, index is left unchanged, except that spaces are still skipped if you set the skipSpaces flag. If base>36, parsing can succeed but digits above 35 (Z) cannot occur in the output number. If the input number cannot fit in 'result', the return value is false but index increases anyway, and 'result' is a bitwise truncated version of the number.

When parsing input such as "12.34", the parser stops and returns true at the dot (with a result of 12 in this case).

Referenced by Loyc.G.TryParseFloatParts(), and Loyc.G.TryParseInt().

static char Loyc.G.UnescapeChar ( string  s,
ref int  i 
)
inlinestatic

Unescapes a single character of a string, e.g. int = 3; UnescapeChar("foo\\n", ref i) == '
'
. Returns the character at 'index' if it is not a backslash, or if it is a backslash but no escape sequence could be discerned.

Exceptions
IndexOutOfRangeExceptionThe index was invalid.
static string Loyc.G.UnescapeCStyle ( string  s)
inlinestatic

Unescapes a string that uses C-style escape sequences, e.g. "\n\r" becomes "
".

static string Loyc.G.UnescapeCStyle ( string  s,
int  index,
int  length,
bool  removeUnnecessaryBackslashes 
)
inlinestatic

Unescapes a string that uses C-style escape sequences, e.g. "\n\r" becomes "
".

Parameters
removeUnnecessaryBackslashesCauses the backslash before an unrecognized escape sequence to be removed, e.g. "\z" => "z".
static bool Loyc.G.Verify ( bool  condition)
inlinestatic

Same as Debug.Assert except that the argument is evaluated even in a Release build.