minid.utils
This module holds a variety of utility functions used throughout MiniD. This
module doesn't (and shouldn't) depend on the rest of the library in any way,
and as such can't hold implementation-specific functionality. For that, look
in the minid.misc module.
License:
Copyright (c) 2008 Jarrett Billingsley
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
- bool
startsWith
(T)(T[] string, T[] pattern);
- See if a string starts with another string. Useful.
- bool
endsWith
(T)(T[] string, T[] pattern);
- See if a string ends with another string. Also useful.
- bool
contains
(T)(T[] arr, T elem);
- See if an array
contains
an item.
- int
Compare3
(T)(T a, T b);
- Compare two values, a and b, using < and >. Returns -1 if a < b, 1 if a > b, and 0 otherwise.
- int
scmp
(char[] s1, char[] s2);
- Compares char[] strings stupidly (just by character value, not lexicographically).
- uint
verify
(char[] s);
- Verifies that the given UTF-8 string is well-formed and returns the length in codepoints.
- char[]
uniSlice
(char[] s, uint lo, uint hi);
- Slice a UTF-8 string using codepoint indices.
- dchar
uniCharAt
(char[] s, uint idx);
- Get the character in a UTF-8 string at the given codepoint index.
- uint
uniCPIdxToByte
(char[] s, uint fake);
- Convert a codepoint index into a UTF-8 string into a byte index.
- uint
uniByteIdxToCP
(char[] s, uint fake);
- Convert a byte index into a UTF-8 string into a codepoint index.
- template
isStringType
(T)
- Metafunction to see if a given type is one of char[], wchar[] or dchar[].
- template
isArrayType
(T)
- Sees if a type is an array.
- template
isAAType
(T)
- Sees if a type is an associative array.
- template
realType
(T)
- Get to the bottom of any chain of typedefs! Returns the first non-typedef'ed type.
- template
FOURCC
(char[] name)
- Make a
FOURCC
code out of a four-character string. This is I guess for little-endian platforms..
- template
MakeVersion
(uint major,uint minor)
- Make a version with the major number in the upper 16 bits and the minor in the lower 16 bits.
- template
NameOfFunc
(alias f)
- Gets the name of a function alias.
- template
QSort
(alias Pred,List...)
- Given a predicate template and a tuple, sorts the tuple. I'm not sure how quick it is, but it's probably fast enough
for sorting most tuples, which hopefully won't be that long. The predicate template should take two parameters of the
same type as the tuple's elements, and return <0 for A < B, 0 for A == B, and >0 for A > B (just like opCmp).
- template
isExpressionTuple
(T...)
- A useful template that somehow is in Phobos but no Tango. Sees if a tuple is composed
entirely of expressions or aliases.
- template
FieldNames
(S,int idx = 0)
- For a given struct, gets a tuple of the names of its fields.
I have absolutely no idea if what I'm doing here is in any way legal. I more or less discovered
that the compiler gives access to this info in odd cases, and am just exploiting that. It would
be fantastic if the compiler would just tell us these things, but alas, we have to rely on
seemingly-buggy undefined behavior. Sigh.
- template
MinArgs
(alias func)
- Given an alias to a function, this will give the minimum legal number of arguments it can be called with.
Even works for aliases to class methods. Note, however, that this isn't smart enough to detect the difference
between, say, "void foo(int x, int y = 10)" and "void foo(int x) ... void foo(int x, int y)". There might
be a difference, though, so be cautious.
- template
InitsOf
(T...)
- Given a type tuple, this will give an expression tuple of all the .init values for each type.
- template
NameOfType
(T)
- Given a class or struct type, gets its name. This really only exists to mask potential oddities with the
way the compiler reports this info (for example, DMD used to insert a space before struct names, but that
no longer seems to happen..).
|