Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/tgstation/-tg-station int…
Browse files Browse the repository at this point in the history
…o CookingOverhaul
  • Loading branch information
phil235 committed Feb 5, 2015
2 parents a296e8f + 7accea7 commit e4464b3
Show file tree
Hide file tree
Showing 58 changed files with 1,359 additions and 630 deletions.
75 changes: 53 additions & 22 deletions code/__HELPERS/text.dm
Expand Up @@ -37,12 +37,12 @@
return t

//Removes a few problematic characters
/proc/sanitize_simple(var/t,var/list/repl_chars = list("\n"="#","\t"="#",""=""))
/proc/sanitize_simple(var/t,var/list/repl_chars = list("\n"="#","\t"="#"))
for(var/char in repl_chars)
var/index = findtext(t, char)
while(index)
t = copytext(t, 1, index) + repl_chars[char] + copytext(t, index+1)
index = findtext(t, char)
index = findtext(t, char, index+1)
return t

//Runs byond's sanitization proc along-side sanitize_simple
Expand Down Expand Up @@ -147,32 +147,63 @@

return t_out

//this proc strips html properly, but it's not lazy like the other procs.
//this means that it doesn't just remove < and > and call it a day. seriously, who the fuck thought that would be useful.
//this proc strips html properly, this means that it removes everything between < and >, and between "http" and "://"
//also limit the size of the input, if specified to
/proc/strip_html_properly(var/input,var/max_length=MAX_MESSAGE_LEN)
if(!input)
return
var/opentag = 1 //These store the position of < and > respectively.
var/closetag = 1
while(1)
opentag = findtext(input, "<")
closetag = findtext(input, ">")
if(closetag && opentag)
if(closetag < opentag)
input = copytext(input, (closetag + 1))
else
input = copytext(input, 1, opentag) + copytext(input, (closetag + 1))
else if(closetag || opentag)
if(opentag)
input = copytext(input, 1, opentag)
else
input = copytext(input, (closetag + 1))
else
break

if(max_length)
input = copytext(input,1,max_length)
return input

var/sanitized_output
var/next_html_tag = findtext(input, "<")
var/next_http = findtext(input, "http", 1, next_html_tag)

//the opening and closing of the expression to skip, e.g '<' and '>'
var/opening = non_zero_min(next_html_tag, next_http)
var/closing

sanitized_output = copytext(input, 1, opening)

while(next_html_tag || next_http)

//we treat < ... >
if(opening == next_html_tag)
closing = findtext(input, ">", opening + 1)
if(closing)
next_html_tag = findtext(input, "<", closing)
next_http = findtext(input, "http", closing, next_html_tag)
else //no matching ">"
next_html_tag = 0

//we treat "http(s)://"
else
closing = findtext(input, "://", opening + 1)
if(closing)
closing += 2 //skip these extra //
next_http = findtext(input, "http", closing)
next_html_tag = findtext(input, "<", closing, next_http)
else //no matching "://"
next_http = 0

//check if we've something to skip
if(closing)
opening = non_zero_min(next_html_tag, next_http)
sanitized_output += copytext(input, closing + 1, opening)

sanitized_output += copytext(input, opening) //don't forget the remaining text

return sanitized_output

//strip_html_properly helper proc that returns the smallest non null of two numbers
//or 0 if they're both null (needed because of findtext returning 0 when a value is not present)
/proc/non_zero_min(var/a, var/b)
if(!a)
return b
if(!b)
return a
return (a < b ? a : b)

/*
* Text searches
Expand Down
2 changes: 1 addition & 1 deletion code/_globalvars/station.dm
@@ -1,4 +1,4 @@
var/global/obj/effect/datacore/data_core = null
var/global/datum/datacore/data_core = null
//var/global/defer_powernet_rebuild = 0 // true if net rebuild will be called manually after an event
//Noble idea, but doing this made GC fail. The gains from waiting on deffering are lost by using del()

Expand Down

0 comments on commit e4464b3

Please sign in to comment.