Giter Site home page Giter Site logo

Comments (2)

r-lyeh-archived avatar r-lyeh-archived commented on August 29, 2024

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

from sole.

dan-ryan avatar dan-ryan commented on August 29, 2024

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

from sole.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.