Enhanced C#
Language of your choice: library documentation
Nested classes | Public static fields | Public Member Functions | 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.
 

Public Member Functions

delegate void ActionRefT< T > (ref T arg)
 

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, ActionRefT< T > action)
 Calls action(ref 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 R Do< T, R > (this T obj, Func< T, R > action)
 Returns action(obj). This method lets you embed statements in any expression. 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 int DecodeBase64Digit (char digit, string digit62="+-.~", string digit63="/_,")
 
static char EncodeBase64Digit (int digit, char digit62='+', char digit63='/')
 
static WordWrapCharType GetWordWrapCharType (int c)
 This function controls the default character categorization used by overloads of WordWrap(string, int, Func<int, WordWrapCharType>). More...
 
static List< string > WordWrap (string paragraph, int lineWidth)
 Breaks a paragraph into lines using a simple word-wrapping algorithm. More...
 
static List< string > WordWrap (IEnumerable< Pair< int, int >> paragraph, int lineWidth, Func< int, WordWrapCharType > getCharType=null)
 Breaks a paragraph into lines using a simple word-wrapping algorithm. More...
 
static bool IsInRangeExcludeHi (this int num, int lo, int hi)
 Returns true if num is between lo and hi, excluding hi but not lo. More...
 
static bool IsInRange (this int num, int lo, int hi)
 Returns true if num is between lo and hi. More...
 
static int PutInRange (this int n, int min, int max)
 Returns num clamped to the range min and max. More...
 
static bool IsInRangeExcludeHi (this uint num, uint lo, uint hi)
 Returns true if num is between lo and hi, excluding hi but not lo. More...
 
static bool IsInRange (this uint num, uint lo, uint hi)
 Returns true if num is between lo and hi. More...
 
static uint PutInRange (this uint n, uint min, uint max)
 Returns num clamped to the range min and max. More...
 
static bool IsInRangeExcludeHi (this long num, long lo, long hi)
 Returns true if num is between lo and hi, excluding hi but not lo. More...
 
static bool IsInRange (this long num, long lo, long hi)
 Returns true if num is between lo and hi. More...
 
static long PutInRange (this long n, long min, long max)
 Returns num clamped to the range min and max. More...
 
static bool IsInRangeExcludeHi (this ulong num, ulong lo, ulong hi)
 Returns true if num is between lo and hi, excluding hi but not lo. More...
 
static bool IsInRange (this ulong num, ulong lo, ulong hi)
 Returns true if num is between lo and hi. More...
 
static ulong PutInRange (this ulong n, ulong min, ulong max)
 Returns num clamped to the range min and max. More...
 
static bool IsInRangeExcludeHi (this float num, float lo, float hi)
 Returns true if num is between lo and hi, excluding hi but not lo. More...
 
static bool IsInRange (this float num, float lo, float hi)
 Returns true if num is between lo and hi. More...
 
static float PutInRange (this float n, float min, float max)
 Returns num clamped to the range min and max. More...
 
static bool IsInRangeExcludeHi (this double num, double lo, double hi)
 Returns true if num is between lo and hi, excluding hi but not lo. More...
 
static bool IsInRange (this double num, double lo, double hi)
 Returns true if num is between lo and hi. More...
 
static double PutInRange (this double n, double min, double max)
 Returns num clamped to the range min and max. More...
 
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

◆ BareHtmlEntityNameForAscii()

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().

◆ CountOnes() [1/2]

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().

◆ CountOnes() [2/2]

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().

◆ Do< T, R >()

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

Returns action(obj). This method lets you embed statements in any expression.

◆ GetWordWrapCharType()

static WordWrapCharType Loyc.G.GetWordWrapCharType ( int  c)
inlinestatic

This function controls the default character categorization used by overloads of WordWrap(string, int, Func<int, WordWrapCharType>).

◆ IsInRange() [1/6]

static bool Loyc.G.IsInRange ( this double  num,
double  lo,
double  hi 
)
inlinestatic

Returns true if num is between lo and hi.

◆ IsInRange() [2/6]

static bool Loyc.G.IsInRange ( this float  num,
float  lo,
float  hi 
)
inlinestatic

Returns true if num is between lo and hi.

◆ IsInRange() [3/6]

static bool Loyc.G.IsInRange ( this int  num,
int  lo,
int  hi 
)
inlinestatic

Returns true if num is between lo and hi.

◆ IsInRange() [4/6]

static bool Loyc.G.IsInRange ( this long  num,
long  lo,
long  hi 
)
inlinestatic

Returns true if num is between lo and hi.

◆ IsInRange() [5/6]

static bool Loyc.G.IsInRange ( this uint  num,
uint  lo,
uint  hi 
)
inlinestatic

Returns true if num is between lo and hi.

◆ IsInRange() [6/6]

static bool Loyc.G.IsInRange ( this ulong  num,
ulong  lo,
ulong  hi 
)
inlinestatic

Returns true if num is between lo and hi.

◆ IsInRangeExcludeHi() [1/6]

static bool Loyc.G.IsInRangeExcludeHi ( this double  num,
double  lo,
double  hi 
)
inlinestatic

Returns true if num is between lo and hi, excluding hi but not lo.

◆ IsInRangeExcludeHi() [2/6]

static bool Loyc.G.IsInRangeExcludeHi ( this float  num,
float  lo,
float  hi 
)
inlinestatic

Returns true if num is between lo and hi, excluding hi but not lo.

◆ IsInRangeExcludeHi() [3/6]

static bool Loyc.G.IsInRangeExcludeHi ( this int  num,
int  lo,
int  hi 
)
inlinestatic

Returns true if num is between lo and hi, excluding hi but not lo.

◆ IsInRangeExcludeHi() [4/6]

static bool Loyc.G.IsInRangeExcludeHi ( this long  num,
long  lo,
long  hi 
)
inlinestatic

Returns true if num is between lo and hi, excluding hi but not lo.

◆ IsInRangeExcludeHi() [5/6]

static bool Loyc.G.IsInRangeExcludeHi ( this uint  num,
uint  lo,
uint  hi 
)
inlinestatic

Returns true if num is between lo and hi, excluding hi but not lo.

◆ IsInRangeExcludeHi() [6/6]

static bool Loyc.G.IsInRangeExcludeHi ( this ulong  num,
ulong  lo,
ulong  hi 
)
inlinestatic

Returns true if num is between lo and hi, excluding hi but not lo.

◆ Log2Floor()

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().

◆ MakeValidFileName()

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 "_".

◆ PutInRange() [1/6]

static double Loyc.G.PutInRange ( this double  n,
double  min,
double  max 
)
inlinestatic

Returns num clamped to the range min and max.

◆ PutInRange() [2/6]

static float Loyc.G.PutInRange ( this float  n,
float  min,
float  max 
)
inlinestatic

Returns num clamped to the range min and max.

◆ PutInRange() [3/6]

static int Loyc.G.PutInRange ( this int  n,
int  min,
int  max 
)
inlinestatic

Returns num clamped to the range min and max.

◆ PutInRange() [4/6]

static long Loyc.G.PutInRange ( this long  n,
long  min,
long  max 
)
inlinestatic

Returns num clamped to the range min and max.

◆ PutInRange() [5/6]

static uint Loyc.G.PutInRange ( this uint  n,
uint  min,
uint  max 
)
inlinestatic

Returns num clamped to the range min and max.

◆ PutInRange() [6/6]

static ulong Loyc.G.PutInRange ( this ulong  n,
ulong  min,
ulong  max 
)
inlinestatic

Returns num clamped to the range min and max.

◆ ToComparison< T >() [1/2]

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> 

◆ ToComparison< T >() [2/2]

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

Converts an IComparer<T> to a Comparison<T>.

◆ ToComparisonFunc< T >()

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

Converts an IComparer<T> to a Func(T,T,int).

◆ True()

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

◆ True< T >()

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.

◆ Verify()

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.MSet< Symbol >.Toggle(), and Loyc.Syntax.Les.Les2Lexer.UnescapeString().

◆ With< T >() [1/3]

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)));

◆ With< T >() [2/3]

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

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

◆ With< T >() [3/3]

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

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

◆ WordWrap() [1/2]

static List<string> Loyc.G.WordWrap ( IEnumerable< Pair< int, int >>  paragraph,
int  lineWidth,
Func< int, WordWrapCharType getCharType = null 
)
inlinestatic

Breaks a paragraph into lines using a simple word-wrapping algorithm.

Parameters
paragraphA sequence of characters that will be treated as a paragraph to be broken into lines as necessary. The first item in each pair is a 21-bit unicode character; the second item is the width of that character, e.g. in pixels. The width must be non-negative and no more than half of int.MaxValue.
lineWidthLine width. The unit used here is the same as the unit of the second item in each pair, e.g. pixels.
getCharTypeA function that determines how a character is relevant to the wrapping operation; see WordWrapCharType. This function accepts 21-bit unicode characters from paragraph.
Returns
A list of lines, each not too long.

This algorithm may be unsuitable for some non-English languages, in which adjacent characters do not have independent widths because of the way they combine. It may still be useful if you only need to make sure lines don't get too long.

Characters higher than 0xFFFF are converted into UTF-16 surrogate pairs.

Whitespace characters at the end of a line are not counted toward the line length limit. As a result, strings in the output list can be longer than lineWidth unless you trim off spaces afterward.

If the input contains a newline character, a line break occurs but the newline is preserved, e.g. "Foo\nBar" causes output like { "Foo\n", "Bar" }. Depending on how you intend to use the output, you may need to trim the newline from the end.

By default, lines can be broken at the soft hyphen '\u00AD', in which case it is advisable for the caller to replace the trailing '\u00AD' with '-' before drawing to ensure that the hyphen is actually displayed on the screen. For simplicity, this replacement is not part of the wrapping algorithm itself.

◆ WordWrap() [2/2]

static List<string> Loyc.G.WordWrap ( string  paragraph,
int  lineWidth 
)
inlinestatic

Breaks a paragraph into lines using a simple word-wrapping algorithm.

Parameters
paragraphText to be broken apart if necessary.
lineWidthLine width in characters.
Returns
A list of lines, each not too long.

The other overload of this function is more flexible, e.g. it supports variable-width characters.

This algorithm may be unsuitable for some non-English languages, in which adjacent characters do not have independent widths because of the way they combine. It may still be useful if you only need to make sure lines don't get too long.

Whitespace characters (including tabs and zero-width spaces) at the end of a line are not counted toward the line length limit. As a result, strings in the output list can be longer than lineWidth unless you trim spaces afterward.

If the input contains a newline character, a line break occurs but the newline is preserved, e.g. "Ann\nBob" causes output like { "Ann\n", "Bob" }. Depending on how you intend to use the output, you may need to trim the newline from the end.

By default, lines can be broken at the soft hyphen '\u00AD', in which case it is advisable for the caller to replace the trailing '\u00AD' with '-' before drawing to ensure that the hyphen is actually displayed on the screen. For simplicity, this replacement is not part of the wrapping algorithm itself.