Make your own free website on Tripod.com

MKoD - D Programming Language

isGUID function - code-name isGUID.d

Very Kool! Checking a string if it's a valid GUID

/+******************************************************************
 * Program       : isGUID.exe ( needs -createmain )
 * Source        : isGUID.d
 * Author        : David L. 'SpottedTiger' Davis
 * Created Date  : 28.Jul.04 Compiled and Tested with dmd v0.97
 *               : 28.Feb.05 Compiled and Tested with dmd v0.114
 *               : 04.Jun.06 Compiled and Tested with dmd v0.160
 *               : 10.Jan.07 Compiled and Tested with dmd v1.0
 * Modified Date : N/A
 * Requirements  : std.string alone, and with main() also needs std.stdio
 * License       : Public Domain 
 *******************************************************************
 *
 * To Compile: C:\dmd\MKoD_ex>dmd isGUID.d -debug=createmain
 +/
debug( createmain ) private import std.stdio;
private import std.string;

/+
 ' GUID Example: {52D176D7-9C27-4D73-935B-222A5D4FF2A2}
 ' If the GUID string passed in is the correct format, then return true;
 +/
public bool isGUID 
( 
    in char[] sGUID
) 
{
    const char[] ALLOWEDCHARS = "0123456789ABCDEF{}-";
    
    // Check the total length
    if ( sGUID.length != 38 ) return false;
    
    // Check for the end brackets
    if ( sGUID[ 0 ] != '{' ) return false;
    if ( sGUID[ sGUID.length - 1 ] != '}' ) return false;
    
    // Check to the dashes
    if ( sGUID[  9 ] != '-' ) return false;
    if ( sGUID[ 14 ] != '-' ) return false;
    if ( sGUID[ 19 ] != '-' ) return false;
    if ( sGUID[ 24 ] != '-' ) return false;
    
    // Check that the remaining characters are in Hex
    for( int ix = 0; ix < sGUID.length; ix++ )
    {
        if ( find( ALLOWEDCHARS, sGUID[ ix ] ) == -1 ) return false;
    }    
    
    // If everything passes above return true
    return true;
    
} // bool isGUID( in char[] )

debug( createmain )
{
int main()
{
    writefln( "isGUID( \"{52D176D7-9C27-4D73-935B-222A5D4FF2A2}\" )=%b", isGUID( "{52D176D7-9C27-4D73-935B-222A5D4FF2A2}" ) );
    return 0;
    
} // end int main()
} // end debug( createmain )
C:\dmd\MKOD_ex>..\bin\dmd isguid.d -debug=createmain
C:\dmd\bin\..\..\dm\bin\link.exe isguid,,,user32+kernel32/noi;

C:\dmd\MKOD_ex>isguid
isGUID( "{52D176D7-9C27-4D73-935B-222A5D4FF2A2}" )=1

C:\dmd\MKOD_ex>
Mars: fourth Rock from the Sun.