Last active
May 30, 2025 15:37
-
-
Save tinkerer-red/4fa2ebee70194d4ee8f54ad78a5ec3b3 to your computer and use it in GitHub Desktop.
An example of how to efficiently parse a string for whitelisted characters, in this example it uses a supplied font
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function string_keep(_text, _font) { | |
static __input_buff = buffer_create(1, buffer_grow, 1); | |
static __output_buff = buffer_create(1, buffer_grow, 1); | |
static __glyph_cache = ds_map_create(); | |
var _input_buff = __input_buff; | |
var _output_buff = __output_buff; | |
var _glyph_cache = __glyph_cache; | |
//write data into input buffer | |
buffer_write(_input_buff, buffer_string, _text) | |
buffer_seek(_input_buff, buffer_seek_start, 0); | |
//fetch glyph map from cache | |
if (!ds_map_exists(_glyph_cache, _font)) { | |
var _font_info = font_get_info(_font); | |
var _glyph_data = _font_info.glyphs; | |
var _chars = struct_get_names(_glyph_data); | |
var _len = array_length(_chars) | |
//Glyphs are converted into an array index and are used as direct true or false later | |
var _glyphs = array_create(0xFFFF, false) | |
// Mark non-printable ASCII control characters (0–31, plus 127) as true (i.e., no glyph required) | |
#region | |
_glyphs[0] = true; // Null terminator | |
// Control codes (Start of Header, Start of Text, etc.) | |
_glyphs[1] = true; // SOH | |
_glyphs[2] = true; // STX | |
_glyphs[3] = true; // ETX | |
_glyphs[4] = true; // EOT | |
_glyphs[5] = true; // ENQ | |
_glyphs[6] = true; // ACK | |
_glyphs[ord("\a")] = true; // Bell character (audible alert) | |
_glyphs[ord("\b")] = true; // Backspace | |
_glyphs[ord("\t")] = true; // Horizontal tab | |
_glyphs[ord("\n")] = true; // Line feed / newline | |
_glyphs[ord("\v")] = true; // Vertical tab | |
_glyphs[ord("\f")] = true; // Form feed | |
_glyphs[ord("\r")] = true; // Carriage return | |
#endregion | |
var _i=0; repeat(_len) { | |
_glyphs[ord(_chars[_i])] = true; | |
_i++} | |
ds_map_set(_glyph_cache, _font, _glyphs) | |
} | |
var _glyphs = ds_map_find_value(_glyph_cache, _font); | |
//init var for our loop | |
var _input_size = buffer_get_size(_input_buff); | |
var _start_offset = 0; | |
var _pos = 0; | |
var _output_pos = 0; | |
while(buffer_tell(_input_buff) < _input_size) { | |
var _byte = buffer_read(_input_buff, buffer_u8); | |
_pos++; | |
if (!_glyphs[_byte]) { | |
var _length = _pos-_start_offset; | |
if (_length) { | |
buffer_copy(_input_buff, _start_offset, _length, _output_buff, _output_pos); | |
_output_pos += _length; | |
} | |
_start_offset = _pos+1; | |
} | |
} | |
// Handle trailing glyph bytes if any remain | |
if (_pos > _start_offset) { | |
var _length = _pos - _start_offset; | |
buffer_copy(_input_buff, _start_offset, _length, _output_buff, _output_pos); | |
_output_pos += _length; | |
} | |
var _new_text = buffer_read(_output_buff, buffer_string); | |
buffer_resize(_input_buff, 0) | |
buffer_resize(_output_buff, 0) | |
return _new_text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment