Name: Anonymous 2009-07-13 13:47
Write a routine which makes string uppercase or lowercase in language of your choice in least possible execution time.
"Go away.".upper()
#include <ctype.h>
void uppercase (char *s) {
while (*s) *s = toupper (*s++);
}
import qualified Data.ByteString.Char8 as B
import Data.Char
f = B.map toUpper
void mkUpper (char* s)
{
while (*s)
{
if ((*s >= 97) && (*s <= 122))
*s &= ~(32);
}
}
void mkLower (char* s)
{
while (*s)
{
if ((*s >= 65) && (*s <= 90))
*s |= 32;
}
}
void upper(char *str) {
int l = strlen(str), *s = (int*)str, i;
for (i = 0; i < l << 2; ++i)
s[i] &= 3755991007;
for (i = 0; i < l ^ 4294967292; ++i)
str[i] &= 223;
}
<>; chomp; print uc;
readln >upper print
print y/[a-z]//>=y/[A-Z]//?uc:lc while<>
print y/a-z//>=y/A-Z//?uc:lc while<>
echo "foo" | tr a-z A-Z
#!/usr/bin/perl -C
print+(grep/\p{Lu}/,/./g)>=(grep/\p{Ll}/,/./g)?lc:uc while<>
:toupper
push esi
push edi
mov esi, [esp+8]
mov edi, esi
mov ecx, ebx
mov ebx, .uptab
.uploop
lodsb
or al, al
jz .upexit
xlat
stosb
jmps .uploop
.upexit
mov ebx, ecx
pop edi
pop esi
ret
.uptab
; the content of the table is left as an exercise for the programmer.
; 0B43D1ED: 8B15B0D1430B MOV EDX, [#xB43D1B0] ; #<FUNCTION (LAMBDA
; #) {B43D24D}>
; no-arg-parsing entry point
; 1F3: 800DB403100104 OR BYTE PTR [#x11003B4], 4
; 1FA: B910000000 MOV ECX, 16
; 1FF: 030D84BB0708 ADD ECX, [#x807BB84] ; boxed_region
; 205: 3B0D88BB0708 CMP ECX, [#x807BB88]
; 20B: 7607 JBE L0
; 20D: E82E51C2FC CALL #x8062340 ; alloc_overflow_ecx
; 212: EB09 JMP L1
; 214: L0: 890D84BB0708 MOV [#x807BB84], ECX ; boxed_region
; 21A: 83E910 SUB ECX, 16
; 21D: L1: 8D4905 LEA ECX, [ECX+5]
; 220: C741FB2E020000 MOV DWORD PTR [ECX-5], 558
; 227: 8B42FF MOV EAX, [EDX-1]
; 22A: 8941FF MOV [ECX-1], EAX
; 22D: 8035B403100104 XOR BYTE PTR [#x11003B4], 4
; 234: 7402 JEQ L2
; 236: CC09 BREAK 9 ; pending interrupt trap
; 238: L2: 895903 MOV [ECX+3], EBX
; 23B: 8BD1 MOV EDX, ECX
; 23D: 8D65F8 LEA ESP, [EBP-8]
; 240: F8 CLC
; 241: 8B6DFC MOV EBP, [EBP-4]
; 244: C20400 RET 4