-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add method to check if string is a valid guid #17
Comments
Hey zammbi, please update to latest source and test following implementation before merging: #include "sole.hpp"
bool is_valid( const char *str ) {
/*
unsigned samples[] = {
sizeof("oCRsAqD-BMfTOs8VvjA"), // [19]
sizeof("QN2LEuUg-B7DNx1YIcnz"), // [20]
sizeof("oTDTokEuR-Dm8Lm3zjiQu"), // [21]
sizeof("Bh3tngmuiH-BfzbduiSRvT"), // [22]
sizeof("G2anmplueET-Bpr24ZCKSlg"), // [23]
sizeof("1581b866-2257-496c-b1b2-3a855b8a32e8") // [36]
};
*/
int len = strlen(str);
int maybe = 0;
if( len == 36 ) {
maybe = ( str[8] == '-' && str[13] == '-' && str[18] == '-' && str[23] == '-' ) &&
( str[14] == '4' || str[14] == '1' || str[14] == '0' );
}
else
if( len >= 19 && len <= 23 ) {
maybe = str[len - 12] == '-';
}
if( maybe ) {
sole::uuid x = sole::rebuild(str);
return x.ab && x.cd;
}
return false;
}
#include <assert.h>
int main() {
assert(!is_valid("hello world") );
assert( is_valid("oCRsAqD-BMfTOs8VvjA")); // [19]
assert( is_valid("QN2LEuUg-B7DNx1YIcnz")); // [20]
assert( is_valid("oTDTokEuR-Dm8Lm3zjiQu")); // [21]
assert( is_valid("Bh3tngmuiH-BfzbduiSRvT")); // [22]
assert( is_valid("G2anmplueET-Bpr24ZCKSlg")); // [23]
assert( is_valid("1581b866-2257-496c-b1b2-3a855b8a32e8")); // [36]
assert(!is_valid("158lb866-2257-496c-b1b2-3a855b8a32e8")); // [36]
assert(!is_valid("1581b866-2257-596c-b1b2-3a855b8a32e8")); // [36]
assert(!is_valid("1581b866-2257-496c-b1b2-3a855b8a32e8 ")); // [36]
} If that's good enough then I'll commit it (properly) anytime soon :) |
Thanks for a quick solution. |
I want to check if a guid string is valid. Currently I've been doing
sole::rebuild(guidString)
but maybe there is a more optimised way.An extra method like
sole::isValid
would come in handy.The text was updated successfully, but these errors were encountered: