# New Ticket Created by Kay-Uwe Huell # Please include the string: [perl #39858] # in the subject line of all future correspondence about this issue. # <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=39858 >
Hi parrot, here is a more elegant function for 'hex_to_int' in examples/io/httpd.pir. Regards, kiwi
Index: CREDITS =================================================================== --- CREDITS (Revision 13335) +++ CREDITS (Arbeitskopie) @@ -282,6 +282,8 @@ N: Juergen Boemmels D: Parrot I/O; macro stuff in assembler. +N: Kay-Uwe 'kiwi' Hüll + N: Kenneth A Graves D: yield and other PIR improvements Index: examples/io/httpd.pir =================================================================== --- examples/io/httpd.pir (Revision 13335) +++ examples/io/httpd.pir (Arbeitskopie) @@ -44,6 +44,11 @@ =cut +.const string CRLF = "\r\n" +.const string CRLFCRLF = "\r\n\r\n" +.const string LFLF = "\n\n" +.const string CRCR = "\r\r" + .sub main :main .local pmc sock, work, fp .local pmc fp # read requested files from disk @@ -83,15 +88,15 @@ if ret <= 0 goto SERVE_REQ concat req, buf - index pos, req, "\r\n\r\n" + index pos, req, CRLFCRLF # print "\npos1:" # print pos if pos >= 0 goto SERVE_REQ - index pos, req, "\n\n" + index pos, req, LFLF # print "\npos2:" # print pos if pos >= 0 goto SERVE_REQ - index pos, req, "\r\r" + index pos, req, CRCR # print "\npos3:" # print pos if pos >= 0 goto SERVE_REQ @@ -137,14 +142,16 @@ unless fp goto SERVE_404 read file_content, fp, 65535 - rep = "HTTP/1.x 200 OK\n" - concat rep, "Server: Parrot-httpd/0.1\n" - concat rep, "Content-Length: " + rep = "HTTP/1.x 200 OK" + rep .= CRLF + rep .= "Server: Parrot-httpd/0.1" + rep .= CRLF + rep .= "Content-Length: " length len, file_content temp = to_string (len) - concat rep, temp - concat rep, "\n\n" - concat rep, file_content + rep .= temp + rep .= CRLFCRLF + rep .= file_content send ret, work, rep print "served file '" print url @@ -152,12 +159,16 @@ goto NEXT SERVE_docroot: - rep = "HTTP1/1 301 Moved Permamently\nLocation: /docs/html/index.html\nContent-Length: " + rep = 'HTTP1/1 301 Moved Permamently' + rep .= CRLF + rep .= 'Location: /docs/html/index.html' + rep .= CRLF + rep .= 'Content-Length: ' file_content = "Please go to <a href='docs/html/index.html'>Parrot Documentation</a>." length len, file_content temp = to_string (len) concat rep, temp - concat rep, "\n\n" + concat rep, CRLFCRLF concat rep, file_content send ret, work, rep print "Redirect to 'docs/html/index.hmtl'\n" @@ -168,7 +179,17 @@ goto SERVE_file SERVE_404: - rep = "HTTP1/1 404 Not Found\nContent-Length: 3\n\n404\n" + $S0 = '404 Not found' + $I0 = length $S0 + rep = 'HTTP1/1 404 Not Found' + rep .= CRLF + rep .= 'Content-Length: ' + $S1 = $I0 + rep .= $S1 + rep .= CRLF + rep .= 'Content-Type: text/plain' + rep .= CRLFCRLF + rep .= $S0 print "File not found: '" print url print "'\n" @@ -237,95 +258,41 @@ .sub hex_to_int - .param string in + .param string hex - .local string char - .local int ret - .local int pos - .local int factor - .local int temp - .local int len + hex = upcase hex - ret = 0 - factor = 1 - length len, in - sub pos, len, 1 + .local int len, num_offset, chr_offset, result -NEXT_CHAR: - substr char, in, pos, 1 + num_offset = ord '0', 0 + chr_offset = ord 'A', 0 - if char=="0" goto CHAR0 - if char=="1" goto CHAR1 - if char=="2" goto CHAR2 - if char=="3" goto CHAR3 - if char=="4" goto CHAR4 - if char=="5" goto CHAR5 - if char=="6" goto CHAR6 - if char=="7" goto CHAR7 - if char=="8" goto CHAR8 - if char=="9" goto CHAR9 - if char=="A" goto CHARA - if char=="B" goto CHARB - if char=="C" goto CHARC - if char=="D" goto CHARD - if char=="E" goto CHARE - if char=="F" goto CHARF + len = length hex -CHAR0: - temp = 0 - goto CHAREND -CHAR1: - temp = 1 - goto CHAREND -CHAR2: - temp = 2 - goto CHAREND -CHAR3: - temp = 3 - goto CHAREND -CHAR4: - temp = 4 - goto CHAREND -CHAR5: - temp = 5 - goto CHAREND -CHAR6: - temp = 6 - goto CHAREND -CHAR7: - temp = 7 - goto CHAREND -CHAR8: - temp = 8 - goto CHAREND -CHAR9: - temp = 9 - goto CHAREND -CHARA: - temp = 10 - goto CHAREND -CHARB: - temp = 11 - goto CHAREND -CHARC: - temp = 12 - goto CHAREND -CHARD: - temp = 13 - goto CHAREND -CHARE: - temp = 14 - goto CHAREND -CHARF: - temp = 15 - goto CHAREND + result = 0 + $I0 = 0 + LOOP: + unless $I0 < len goto RETURN + $I1 = ord hex, $I0 + if $I1 < chr_offset goto HAVE_NUMBER -CHAREND: - mul temp, factor, temp - add ret, temp, ret - mul factor, factor, 16 - sub pos, pos, 1 - if pos>=0 goto NEXT_CHAR + HAVE_CHAR: + $I1 -= chr_offset + $I1 += 10 + goto CALC - .return( ret ) + HAVE_NUMBER: + $I1 -= num_offset + + CALC: + result *= 16 + result += $I1 + inc $I0 + + goto LOOP + + RETURN: + print result + print "\n" + .return(result) .end