#include "util.h" #include #include #include #ifdef _WIN32 #include #else #include #endif void error(const char* format, ...) { va_list args; va_start(args, format); vfprintf(stderr, format, args); fputs("\n", stderr); va_end(args); exit(EXIT_FAILURE); } void lovrSleep(double seconds) { #ifdef _WIN32 Sleep((unsigned int)(seconds * 1000)); #else usleep((unsigned int)(seconds * 1000000)); #endif } void* lovrAlloc(size_t size, void (*destructor)(const Ref* ref)) { void* object = malloc(size); if (!object) return NULL; *((Ref*) object) = (Ref) { destructor, 1 }; return object; } void lovrRetain(const Ref* ref) { ((Ref*) ref)->count++; } void lovrRelease(const Ref* ref) { if (--((Ref*) ref)->count == 0 && ref->free) ref->free(ref); } // https://github.com/starwing/luautf8 size_t utf8_decode(const char *s, const char *e, unsigned *pch) { unsigned ch; if (s >= e) { *pch = 0; return 0; } ch = (unsigned char)s[0]; if (ch < 0xC0) goto fallback; if (ch < 0xE0) { if (s+1 >= e || (s[1] & 0xC0) != 0x80) goto fallback; *pch = ((ch & 0x1F) << 6) | (s[1] & 0x3F); return 2; } if (ch < 0xF0) { if (s+2 >= e || (s[1] & 0xC0) != 0x80 || (s[2] & 0xC0) != 0x80) goto fallback; *pch = ((ch & 0x0F) << 12) | ((s[1] & 0x3F) << 6) | (s[2] & 0x3F); return 3; } { int count = 0; /* to count number of continuation bytes */ unsigned res = 0; while ((ch & 0x40) != 0) { /* still have continuation bytes? */ int cc = (unsigned char)s[++count]; if ((cc & 0xC0) != 0x80) /* not a continuation byte? */ goto fallback; /* invalid byte sequence, fallback */ res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ ch <<= 1; /* to test next bit */ } if (count > 5) goto fallback; /* invalid byte sequence */ res |= ((ch & 0x7F) << (count * 5)); /* add first byte */ *pch = res; return count+1; } fallback: *pch = ch; return 1; }