Code: Select all
a$="hal"
For p=1 To Len(a$)
b$+Chr(Asc(Mid(a$,p,1))+1)
Next
Debug b$
Code: Select all
a$="hal"
For p=1 To Len(a$)
b$+Chr(Asc(Mid(a$,p,1))+1)
Next
Debug b$
Code: Select all
Structure CharArray
char.c[0]
EndStructure
a$="hal"
*b.CharArray = @a$
c = Len(a$) -1
For i = 0 To c
*b\char[i] + 1
Next
Debug a$
Code: Select all
Procedure.s InitialCaps(text$)
s=Len(text$)
For p=1 To s
pre=Asc(LCase(Mid(text$,p-1,1))) : cur=Asc(LCase(Mid(text$,p,1)))
If pre<>39 And cur>96 And cur<123 And (p=1 Or pre<97 Or pre>122) : cur-32 : EndIf
n$+Chr(cur)
Next
ProcedureReturn n$
EndProcedure
Code: Select all
a$="hal"
*c.Character = @a$
While *c\c
*c\c + 1
*c + SizeOf(Character)
Wend
Debug a$
Code: Select all
a$="hal"
b$ = Space(Len(a$))
*s.Character = @a$
*d.Character = @b$
While *s\c
*d\c = *s\c + 1
*d + SizeOf(Character)
*s + SizeOf(Character)
Wend
Debug b$
Code: Select all
; While *c\c
_While1:
MOV ebp,dword [p_c]
CMP byte [ebp],0
JE _Wend1
; *c\c + 1
MOV ebp,dword [p_c]
MOVZX ebx,byte [ebp]
INC ebx
PUSH ebx
MOV ebp,dword [p_c]
POP eax
MOV byte [ebp],al
; *c + SizeOf(Character)
INC dword [p_c]
; Wend
JMP _While1
_Wend1:
Code: Select all
; For i = 0 To c
MOV dword [v_i],0
_For1:
MOV eax,dword [v_c]
CMP eax,dword [v_i]
JL _Next2
; *b\char[i] + 1
MOV ebp,dword [p_b]
PUSH ebp
MOV eax,dword [v_i]
POP ebp
ADD ebp,eax
MOVZX ebx,byte [ebp]
INC ebx
PUSH ebx
MOV ebp,dword [p_b]
PUSH ebp
MOV eax,dword [v_i]
POP ebp
ADD ebp,eax
POP eax
MOV byte [ebp],al
; Next
_NextContinue2:
INC dword [v_i]
JNO _For1
_Next2:
Code: Select all
Procedure.s InitialCaps(text$)
Structure CharArray
char.c[0]
EndStructure
text$=LCase(text$)
*b.CharArray=@text$
s=Len(text$)
For p=0 To s
pre=*b\char[p-1] : cur=*b\char[p]
If pre<>39 And cur>96 And cur<123 And (p=0 Or pre<97 Or pre>122) : cur-32 : EndIf
n$+Chr(cur)
Next
ProcedureReturn n$
EndProcedure
Debug InitialCaps("ALL words SHOULD have initial capital letters ONLY!")
Code: Select all
EnableExplicit
Structure CharArray
char.c[0]
EndStructure
Procedure.s InitialCaps(text$)
Protected i, cur, pre, c = Len(text$) -1
Protected b$ = LCase(text$)
Protected *b.CharArray = @b$
Protected n$
For i = 0 To c
pre = *b\char[i -1]
cur = *b\char[i]
If pre <> 39 And cur > 96 And cur < 123 And (i = 0 Or pre < 97 Or pre > 122) : cur - 32 : EndIf
n$ + Chr(cur)
Next
ProcedureReturn n$
EndProcedure
Debug InitialCaps("hal")
Yeah, but both mine and yours are still too slow on 500 KB text. I'm looking for something as fast as the native string commands, which do it in like 10 ms or so. There must be a way. Maybe memory blocks instead. I'll keep trying.ts-soft wrote://edit: to late
Code: Select all
Procedure.s InitialCaps(text$)
Structure CharArray
char.c[0]
EndStructure
Protected s, *b.CharArray, cur, pre, p, previousWasLetter = #False
text$ = LCase(text$)
*b.CharArray = @text$
s = Len(text$)
If s > 0
cur = *b\char[0]
If cur > 96 And cur < 123: cur - 32: previousWasLetter = #True: EndIf
*b\char[0] = cur
s - 1
For p = 1 To s
pre = cur: cur = *b\char[p]
If pre <> 39 And cur > 96 And cur < 123
If Not previousWasLetter
cur - 32
previousWasLetter = #True
EndIf
Else
previousWasLetter = #False
EndIf
*b\char[p] = cur
Next
EndIf
ProcedureReturn text$
EndProcedure
Debug InitialCaps("ALL words SHOULD have initial capital letters ONLY!")
Code: Select all
n$ + Chr(cur)
I know, but that's not the bottleneck, because I removed that line for a test and the procedure was just as slow without it.skywalk wrote:If you need speed, never do this![]()
Code: Select all
n$ + Chr(cur)
Code: Select all
Debug InitialCaps("'hI there'! ev'RY word 'SHOULD' have initial cap's ONLY and ''quotes'' working.")
Huh? Integer comparisons are fast!MachineCode wrote:I know, but that's not the bottleneck, because I removed that line for a test and the procedure was just as slow without it. Therefore, I knew it had to be the comparisons causing the slowdown.
The changes you were needing and the inputs it would be required to work with weren't really specified. To deal with those you need to nail down the input requirements and how to handle them. What about inputs like this: "that's", hypen-ated, under_scored, or a.c.m.e.? So far, I think the code only handles the last one correctly by producing "A.C.M.E." . It will depend on what is desired in each of those conditions as well as nonsense conditions like '-silly'.MachineCode wrote:@Demivec: I could kiss you!Not really. But yours is fast, and does my 500 KB of text in about 20 ms! Thank you!
HOWEVER, it fails with apostrophes... give it this line:
I know the sentence is not grammatically correct, but it's just to show the problem.Code: Select all
Debug InitialCaps("ALL you're words 'SHOULD' have initial cap's and ''quotes'' working.")
Code: Select all
'hI there'! ev'RY word 'SHOULD' have initial cap's ONLY and ''quotes'' working.
Code: Select all
'Hi There'! Ev'ry Word 'Should' Have Initial Cap's Only And ''Quotes'' Working.
Code: Select all
Procedure.s InitialCaps(text$)
Structure CharArray
char.c[0]
EndStructure
Protected s, *b.CharArray, cur, p, wordStarted = #False
text$ = LCase(text$)
*b.CharArray = @text$
s = Len(text$)
If s > 0
s - 1
For p = 0 To s
cur = *b\char[p]
Select cur
Case 97 To 122
If Not wordStarted
cur - 32
wordStarted = #True
EndIf
Case 39, '-', '_' ;apostrophe, hyphen and underscore treated as part of word
Default
wordStarted = #False
EndSelect
*b\char[p] = cur
Next
EndIf
ProcedureReturn text$
EndProcedure
Code: Select all
Procedure.s InitialCaps(text$)
Structure CharArray
char.c[0]
EndStructure
Protected *b.CharArray, cur, wordStarted = #False
*b.CharArray = @text$
p = 0
While *b\char[p] <> 0
cur = *b\char[p]
Select cur
Case 97 To 122
If Not wordStarted
cur - 32
wordStarted = #True
EndIf
Case 65 To 90
If wordStarted
cur + 32
Else
wordStarted = #True
EndIf
Case 39, '-', '_' ;apostrophe, hyphen and underscore treated as part of word
Default
wordStarted = #False
EndSelect
*b\char[p] = cur
p + 1
Wend
ProcedureReturn text$
EndProcedure
Code: Select all
CompilerIf #PB_Compiler_Debugger
Debug InitialCaps("'hI there'! ev'RY word 'SHOULD' have initial cap's ONLY and ''quotes'' working.")
Debug InitialCaps("What about inputs like this: that's, hypen-ated, under_scored, Or a.c.m.e.?")
CompilerElse
Define t, a$, i
For i = 1 To 6500
a$ + "'hI there'! ev'RY word 'SHOULD' have initial cap's ONLY and ''quotes'' working."
Next
t= ElapsedMilliseconds()
b$ = InitialCaps(a$)
t = ElapsedMilliseconds() - t
MessageRequester("Result", Str(t) + " " + Str(Len(b$)))
CompilerEndIf