Kohi Game Engine
kstring_id.h File Reference

This files contains an implementation of kstring_ids. More...

#include "defines.h"

Go to the source code of this file.

Macros

#define INVALID_KSTRING_ID   0
 Represents an invalid kstring_id, which is essentially used to represent a null or empty string. More...
 

Typedefs

typedef u64 kstring_id
 A kstring_id is a string hash made for quick comparisons versus traditional string comparisons. More...
 

Functions

KAPI kstring_id kstring_id_create (const char *str)
 
KAPI const char * kstring_id_string_get (kstring_id stringid)
 

Detailed Description

This files contains an implementation of kstring_ids.

Author
Travis Vroman (travi.nosp@m.s@ko.nosp@m.hieng.nosp@m.ine..nosp@m.com)

kstring_ids are a lightweight system to manage strings within an application for quick comparisons. Each kstring_id is hashed into a unique number stored as a key in a lookup table for later use. This lookup table also stores a copy of the original string. kstring_id are immutable, and thus cannot be changed once internalized into the lookup table, even when reused.

NOTE: kstring_ids are case-sensitive. For a case-insensitive variant, see kname.h

Version
1.0
Date
2024-11-26

Macro Definition Documentation

◆ INVALID_KSTRING_ID

#define INVALID_KSTRING_ID   0

Represents an invalid kstring_id, which is essentially used to represent a null or empty string.

Typedef Documentation

◆ kstring_id

typedef u64 kstring_id

A kstring_id is a string hash made for quick comparisons versus traditional string comparisons.

A kname's hash is generated from a case-sensitive version of a string. The original string may be looked up and retrieved at any time using kstring_id_string_get().

Function Documentation

◆ kstring_id_create()

KAPI kstring_id kstring_id_create ( const char *  str)

Creates a kstring_id for the given string. This creates a hash of the string for quick comparisons. A copy of the original string is maintained within an internal global lookup table, where the hash provided (i.e. kstring_id) is the lookup key. NOTE: kstring_ids are case-sensitive! NOTE: A hash of 0 is never allowed here.

Parameters
strThe source string to use while creating the kstring_id.
Returns
The hashed kstring_id.

◆ kstring_id_string_get()

KAPI const char* kstring_id_string_get ( kstring_id  stringid)

Attempts to get the original string associated with the given kname. This will only work if the name was originally registered in the internal global lookup table.

Parameters
nameThe kstring_id to lookup.
Returns
A constant pointer to the string if found, otherwise 0/null. NOTE: Do NOT free this string!