Enhanced C#
Language of your choice: library documentation
Nested classes | 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 files:

Remarks

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

Note: helper methods for parsing and printing tokens and hex digits have been moved to Loyc.Syntax.ParseHelpers.

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 void Swap (ref dynamic a, ref dynamic b)
 
static bool SortPair< T > (ref T lo, ref T hi, Comparison< T > comp)
 
static bool SortPair< T > (ref T lo, ref T hi)
 
static bool IsOneOf< T > (this T value, T item1, T item2)
 
static bool IsOneOf< T > (this T value, T item1, T item2, T item3)
 
static bool IsOneOf< T > (this T value, T item1, T item2, T item3, T item4)
 
static bool IsOneOf< T > (this T value, T item1, T item2, T item3, T item4, T item5)
 
static bool IsOneOf< T > (this T value, params T[] set)
 
static T With< T > (this T obj, Action< T > action)
 Calls action(obj), then returns the same object. More...
 
static T With< T > (this T obj, Func< T, T > action)
 Returns action(obj). This is similar to the other overload of this method, except that the action has a return value. More...
 
static bool True< T > (T value)
 Returns true. This method has no effect; it is used to do an action in a conditional expression. More...
 
static bool True (Action action)
 This method simply calls the delegate provided and returns true. It is used to do an action in a conditional expression. More...
 
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 bool Verify (bool condition)
 Same as Debug.Assert except that the argument is evaluated even in a Release build. 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. BareHtmlEntityNameForAscii('"') == "quot". More...
 
static int CountOnes (byte x)
 Returns the number of bits that are set in the specified integer. More...
 
static int CountOnes (int x)
 Returns the number of bits that are set in the specified integer. More...
 
static int CountOnes (uint x)
 
static double Int64BitsToDouble (long bits)
 
static long DoubleToInt64Bits (double value)
 
static int Log2Floor (uint x)
 
static int Log2Floor (int x)
 Returns the floor of the base-2 logarithm of x. e.g. 1024 -> 10, 1000 -> 9 More...
 
static double ShiftLeft (double num, int amount)
 
static double ShiftRight (double num, int amount)
 
static bool IsInRangeExcludeHi (this int num, int lo, int hi)
 
static bool IsInRange (this int num, int lo, int hi)
 
static int PutInRange (this int n, int min, int max)
 
static bool IsInRangeExcludeHi (this uint num, uint lo, uint hi)
 
static bool IsInRange (this uint num, uint lo, uint hi)
 
static uint PutInRange (this uint n, uint min, uint max)
 
static bool IsInRangeExcludeHi (this long num, long lo, long hi)
 
static bool IsInRange (this long num, long lo, long hi)
 
static long PutInRange (this long n, long min, long max)
 
static bool IsInRangeExcludeHi (this ulong num, ulong lo, ulong hi)
 
static bool IsInRange (this ulong num, ulong lo, ulong hi)
 
static ulong PutInRange (this ulong n, ulong min, ulong max)
 
static bool IsInRangeExcludeHi (this float num, float lo, float hi)
 
static bool IsInRange (this float num, float lo, float hi)
 
static float PutInRange (this float n, float min, float max)
 
static bool IsInRangeExcludeHi (this double num, double lo, double hi)
 
static bool IsInRange (this double num, double lo, double hi)
 
static double PutInRange (this double n, double min, double max)
 
static bool IsInRangeExcludeHi< T > (this T num, T lo, T hi)
 
static bool IsInRange< T > (this T num, T lo, T hi)
 
static T PutInRange< T > (this T n, T min, T max)
 

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. BareHtmlEntityNameForAscii('"') == "quot".

The complete entity name is an ampersand (&) plus BareHtmlEntityNameForAscii(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.

Referenced by Loyc.Ecs.EcsValidators.SanitizeIdentifier().

static int Loyc.G.CountOnes ( byte  x)
inlinestatic

Returns the number of bits that are set in the specified integer.

Referenced by Loyc.G.Log2Floor(), and Loyc.Collections.Impl.InternalSet< KeyValuePair< K, V > >.Thaw().

static int Loyc.G.CountOnes ( int  x)
inlinestatic

Returns the number of bits that are set in the specified integer.

This is a duplicate of MathEx.CountOnes() needed by Loyc.Collections, which does not have a reference to Loyc.Math.dll which contains MathEx. However this uses a compact SWAR implementation, whereas Loyc.Math uses a potentially faster lookup table.

References Loyc.G.CountOnes().

Referenced by Loyc.G.CountOnes().

static int Loyc.G.Log2Floor ( int  x)
inlinestatic

Returns the floor of the base-2 logarithm of x. e.g. 1024 -> 10, 1000 -> 9

The return value is -1 for an input that is zero or negative.

Some processors have a dedicated instruction for this operation, but the .NET framework provides no access to it.

References Loyc.G.Log2Floor().

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 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 bool Loyc.G.True ( Action  action)
inlinestatic

This method simply calls the delegate provided and returns true. It is used to do an action in a conditional expression.

Returns
True
static bool Loyc.G.True< T > ( value)
inlinestatic

Returns true. This method has no effect; it is used to do an action in a conditional expression.

Parameters
valueIgnored.
Returns
True.
static bool Loyc.G.Verify ( bool  condition)
inlinestatic

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

Referenced by Loyc.Collections.ReversedList< T >.RemoveAll(), Loyc.Collections.MSet< Symbol >.Toggle(), and Loyc.Syntax.Les.Les2Lexer.UnescapeString().

static T Loyc.G.With< T > ( this T  obj,
Action< T >  action 
)
inlinestatic

Calls action(obj), then returns the same object.

Returns
obj

This is the plain-C# equivalent of the with(obj) statement. Compared to the Enhanced C# statement, With() is disadvantageous since it requires a memory allocation to create the closure in many cases, as well as a delegate invocation that probably will not be inlined.

Caution: you cannot mutate mutable structs with this method. Call the other overload of this method if you will be modifying a mutable struct.

Foo(new Person() { Name = "John Doe" }.With(p => p.Commit(dbConnection)));

static T Loyc.G.With< T > ( this T  obj,
Func< T, T >  action 
)
inlinestatic

Returns action(obj). This is similar to the other overload of this method, except that the action has a return value.