ios - Trying to decode HTML characters in objective C is taking forever and 100% cpu usage -
i using piece of code decode few html special characters in xml data
+(nsstring *)getnsstringformhtmlstring:(nsstring *)html { if(system_version_greater_than_or_equal_to(@"7.0")) { nsmutableattributedstring* attrdisplaystring = [[nsmutableattributedstring alloc] initwithdata:[html datausingencoding:nsunicodestringencoding]options:@{nsdocumenttypedocumentattribute: nshtmltextdocumenttype} documentattributes:nil error:nil]; return [attrdisplaystring string]; } return html; }
this code works in handy if using few number of times.
but,
my use case is, need parse junk xml has lot of encoded characters.
like this
<filter name="added_time"> <displayname><![cdata[added time]]></displayname> <value display="dec - 2013"><![cdata[added_time:dec - 2013]]></value> <value display="feb - 2014"><![cdata[added_time:feb - 2014]]></value> <value display="mar - 2014"><![cdata[added_time:mar - 2014]]></value> <value display="apr - 2014"><![cdata[added_time:apr - 2014]]></value> <value display="sep - 2014"><![cdata[added_time:sep - 2014]]></value> <value display="nov - 2014"><![cdata[added_time:nov - 2014]]></value> </filter>
since above piece of code called repeatedly decode every key , values, 100% cpu usage , parsing going on forever. (anyhow gets completed in 2 or 3 minutes of time)
see
apparently,
getnsstringformhtmlstring:
proving costly operation.
help me out!!
i need solution similar task doest consume of time.
best thing can come try out alternative library.
this answer has custom solution made of 2 file , written in c should quick. i'd try running through decode step , try getnsstringfromhtmlstring:
call.
+(nsstring *)getnsstringformhtmlstring:(nsstring *)html { if(system_version_greater_than_or_equal_to(@"7.0")) { nsmutabledata* data = [[html datausingencoding:nsutf8stringencoding] mutablecopy]; char* output = malloc(data.length); char* bytes = data.mutablebytes; decode_html_entities_utf8(bytes, bytes); nsstring* converted = [nsstring stringwithutf8string:output]; nsmutableattributedstring* attrdisplaystring = [[nsmutableattributedstring alloc] initwithdata:[converted datausingencoding:nsunicodestringencoding]options:@{nsdocumenttypedocumentattribute: nshtmltextdocumenttype} documentattributes:nil error:nil]; return [attrdisplaystring string]; } return html; }
no idea if quicker or not have copy when converting mutable nsdata array.
if find it's still slow, can try replace nsdata code directly converting data in html string using - (bool)getbytes:(void *)buffer maxlength:(nsuinteger)maxbuffercount usedlength:(nsuinteger *)usedbuffercount encoding:(nsstringencoding)encoding options:(nsstringencodingconversionoptions)options range:(nsrange)range remainingrange:(nsrangepointer)leftover;
Comments
Post a Comment