Skip to content
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

Open
dan-ryan opened this issue Jun 22, 2017 · 2 comments
Open

Add method to check if string is a valid guid #17

dan-ryan opened this issue Jun 22, 2017 · 2 comments

Comments

@dan-ryan
Copy link

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.

@r-lyeh-archived
Copy link
Owner

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 :)
ty

@dan-ryan
Copy link
Author

dan-ryan commented Jun 22, 2017

Thanks for a quick solution.
However since all my guids should be valid, this method would run slower than just doing the sole::rebuild check. I don't want any object creation. Just to check if it's a valid guid string if possible. I'm trying to optimise a hot spot piece of code :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants