mirror of
https://github.com/CHOMPStation2/CHOMPStation2.git
synced 2025-12-27 02:23:47 +00:00
[MIRROR] Instrument Update (#11645)
Co-authored-by: Selis <12716288+ItsSelis@users.noreply.github.com> Co-authored-by: Kashargul <144968721+Kashargul@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
19b8044f61
commit
d8408a2c59
@@ -601,6 +601,113 @@ GLOBAL_LIST_EMPTY(text_tag_cache)
|
||||
else
|
||||
return trim(html_encode(user_input), max_length)
|
||||
|
||||
#define NO_CHARS_DETECTED 0
|
||||
#define SPACES_DETECTED 1
|
||||
#define SYMBOLS_DETECTED 2
|
||||
#define NUMBERS_DETECTED 3
|
||||
#define LETTERS_DETECTED 4
|
||||
|
||||
/**
|
||||
* Filters out undesirable characters from names.
|
||||
*
|
||||
* * strict - return null immediately instead of filtering out
|
||||
* * allow_numbers - allows numbers and common special characters - used for silicon/other weird things names
|
||||
* * cap_after_symbols - words like Bob's will be capitalized to Bob'S by default. False is good for titles.
|
||||
*/
|
||||
/proc/reject_bad_name(t_in, allow_numbers = FALSE, max_length = MAX_NAME_LEN, ascii_only = TRUE, strict = FALSE, cap_after_symbols = TRUE)
|
||||
if(!t_in)
|
||||
return //Rejects the input if it is null
|
||||
|
||||
var/number_of_alphanumeric = 0
|
||||
var/last_char_group = NO_CHARS_DETECTED
|
||||
var/t_out = ""
|
||||
var/t_len = length(t_in)
|
||||
var/charcount = 0
|
||||
var/char = ""
|
||||
|
||||
// This is a sanity short circuit, if the users name is three times the maximum allowable length of name
|
||||
// We bail out on trying to process the name at all, as it could be a bug or malicious input and we don't
|
||||
// Want to iterate all of it.
|
||||
if(t_len > 3 * MAX_NAME_LEN)
|
||||
return
|
||||
for(var/i = 1, i <= t_len, i += length(char))
|
||||
char = t_in[i]
|
||||
switch(text2ascii(char))
|
||||
|
||||
// A .. Z
|
||||
if(65 to 90) //Uppercase Letters
|
||||
number_of_alphanumeric++
|
||||
last_char_group = LETTERS_DETECTED
|
||||
|
||||
// a .. z
|
||||
if(97 to 122) //Lowercase Letters
|
||||
if(last_char_group == NO_CHARS_DETECTED || last_char_group == SPACES_DETECTED || cap_after_symbols && last_char_group == SYMBOLS_DETECTED) //start of a word
|
||||
char = uppertext(char)
|
||||
number_of_alphanumeric++
|
||||
last_char_group = LETTERS_DETECTED
|
||||
|
||||
// 0 .. 9
|
||||
if(48 to 57) //Numbers
|
||||
if(!allow_numbers) //allow name to start with number if AI/Borg
|
||||
if(strict)
|
||||
return
|
||||
continue
|
||||
number_of_alphanumeric++
|
||||
last_char_group = NUMBERS_DETECTED
|
||||
// ' - .
|
||||
if(39,45,46) //Common name punctuation
|
||||
if(last_char_group == NO_CHARS_DETECTED)
|
||||
if(strict)
|
||||
return
|
||||
continue
|
||||
last_char_group = SYMBOLS_DETECTED
|
||||
|
||||
// ~ | @ : # $ % & * +
|
||||
if(126,124,64,58,35,36,37,38,42,43) //Other symbols that we'll allow (mainly for AI)
|
||||
if(last_char_group == NO_CHARS_DETECTED || !allow_numbers) //suppress at start of string
|
||||
if(strict)
|
||||
return
|
||||
continue
|
||||
last_char_group = SYMBOLS_DETECTED
|
||||
|
||||
//Space
|
||||
if(32)
|
||||
if(last_char_group == NO_CHARS_DETECTED || last_char_group == SPACES_DETECTED) //suppress double-spaces and spaces at start of string
|
||||
if(strict)
|
||||
return
|
||||
continue
|
||||
last_char_group = SPACES_DETECTED
|
||||
|
||||
if(127 to INFINITY)
|
||||
if(ascii_only)
|
||||
if(strict)
|
||||
return
|
||||
continue
|
||||
last_char_group = SYMBOLS_DETECTED //for now, we'll treat all non-ascii characters like symbols even though most are letters
|
||||
|
||||
else
|
||||
continue
|
||||
t_out += char
|
||||
charcount++
|
||||
if(charcount >= max_length)
|
||||
break
|
||||
|
||||
if(number_of_alphanumeric < 2)
|
||||
return //protects against tiny names like "A" and also names like "' ' ' ' ' ' ' '"
|
||||
|
||||
if(last_char_group == SPACES_DETECTED)
|
||||
t_out = copytext_char(t_out, 1, -1) //removes the last character (in this case a space)
|
||||
|
||||
//if(!filter_name_ic(t_out)) FIXME
|
||||
// return
|
||||
|
||||
return t_out
|
||||
|
||||
#undef NO_CHARS_DETECTED
|
||||
#undef SPACES_DETECTED
|
||||
#undef NUMBERS_DETECTED
|
||||
#undef LETTERS_DETECTED
|
||||
|
||||
//Adds 'char' ahead of 'text' until there are 'count' characters total
|
||||
/proc/add_leading(text, count, char = " ")
|
||||
text = "[text]"
|
||||
|
||||
Reference in New Issue
Block a user