MacroUtils.h
// MacroUtils.h
//
// Author David Barrett-Lennard
// (C)opyright Cedanet Pty Ltd 2008
///////////////////////////////////////////////////////////////////////////////////////////////
// mToIdentifier. Used to convert a type into a valid identifier - needed for templates
@runpython
{
# convert the given string into a valid identifier by removing invalid characters
def makeidentifier(s):
i = 0
r = ''
while i < len(s):
c = s[i]
if c == '*':
r = r + '_ptr_'
if c == '&':
r = r + '_ref_'
elif c == '<':
r = r + '_6_'
elif c == '>':
r = r + '_9_'
elif c == '[':
r = r + '_bs_'
elif c == ']':
r = r + '_es_'
elif c == ',':
r = r + '_'
elif c == ':':
r = r + '8'
elif ('0' <= c and c <= '9') or ('a' <= c and c <= 'z') or ('A' <= c and c <= 'Z') or (c == '_'):
r = r + c
i = i+1
return r
}
@defpython mToIdentifier(s) =
{
makeidentifier(@str(s))
}
// Returns the suffix of the string that comes after the right most :, or the whole string if no
// : character is found. E.g. ceda::int8 ---> int8
@defpython int mStripQualification(s) =
{
@str(s).rpartition(':')[2]
}
// Returns the number of characters in the given string
@defpython int mStringLength(s) =
{
len(@str(s))
}
@defpython mGetSubString(s,int i1,int i2) =
{
(@str(s))[i1:i2]
}
@defpython mGetCharInString(s,int i) =
{
(@str(s))[i]
}
@defpython mToUpper(s) =
{
@str(s).upper()
}
@defpython mToLower(s) =
{
@str(s).lower()
}
@defpython mCapitaliseFirstLetter(s) =
{
@str(s).capitalize()
}
@defpython mEatLeadingWhitespace(s) =
{
@str(s).lstrip()
}
@defpython mEatTrailingWhitespace(s) =
{
@str(s).rstrip()
}
// Centre justify s within a string of size width
@defpython string mCentreJustify(s,int width) =
{
@str(s).center(width)
}
// Left justify s within a string of size width
@defpython mLeftJustify(s,int width) =
{
@str(s).ljust(width)
}
// Right justify s within a string of size width
@defpython mRightJustify(s,int width) =
{
@str(s).rjust(width)
}
// Count num substrings t in s
@defpython int mCountSubStrings(s,t) =
{
@str(s).count(@str(t))
}
@defpython int mExpandTabsInString(s,int tabsize) =
{
@str(s).expandtabs(tabsize)
}
// Return the lowest index in s where substring t is found
// Returns -1 if t is not found.
@defpython int mFindLeftMostSubString(s,t) =
{
@str(s).find(@str(t))
}
// Return the greated index in s where substring t is found
// Returns -1 if t is not found.
@defpython int mFindRightMostSubString(s,t) =
{
@str(s).rfind(@str(t))
}
// Returns true if t is a prefix of s
@defpython bool mIsSubStringPrefix(s,t) =
{
@str(s).startswith(@str(t))
}
// Returns true if t is a suffix of s
@defpython bool mIsSubStringSuffix(s,t) =
{
@str(s).endswith(@str(t))
}
// In s replace all occurences of f with r
@defpython mReplaceInString(s,f,r) =
{
@str(s).replace(@str(f),@str(r))
}
@defpython mStripLeadingAndTrailingWhitespace(s) =
{
@str(s).strip()
}
@defpython mPadWithLeadingZeros(s,int width) =
{
@str(s).zfill(width)
}
@def int mBoolToInt(bool x) = @if(x){1}@else{0}
@defpython mSplitLines(s,bool keepends) =
{
@str(s).splitlines(mBoolToInt(keepends))
}
@defpython mSplitWithDelimiter(s,char delimiter) =
{
@str(s).split(delimiter)
}
// Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise.
@defpython bool mIsAlnumString(s) =
{
@str(s).isalnum()
}
// Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.
@defpython bool mIsAlphaString(s) =
{
@str(s).isalpha()
}
// Return true if all characters in the string are digits and there is at least one character, false otherwise.
@defpython bool mIsDigitString(s) =
{
@str(s).isdigit()
}
// Return true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.
@defpython bool mIsLowerCaseString(s) =
{
@str(s).islower()
}
// Return true if there are only whitespace characters in the string and there is at least one character, false otherwise.
@defpython bool mIsWhiteSpaceString(s) =
{
@str(s).isspace()
}
@def bool mMacroDefined(s) =
{
@str(@[@macro@@s]) == "1"
}
@def bool mIsEmpty(v) =
{
@str(v) == ""
}