Quantcast

Jump to content

» «
Photo

[tUt]Some (perhaps!) Useful Str Funcs

2 replies to this topic
MasterHK
  • MasterHK

    کیان حامی‏

  • Members
  • Joined: 20 Mar 2013
  • Iran

#1

Posted 01 January 2014 - 04:21 AM Edited by MasterHK, 02 January 2014 - 03:55 AM.

Hi guys!

Happy new year!

today i want to show you some funcs that may be useful in your scripts.

they are simple and written in cleo but can be be used in scm too.

if you has been worked with VB certainly you have been seen this funcs.

 

Thanks to fastman92. fixed. : )

I think my problem in here comes from this mistake.

 

List of Funcs:

InStr(Searchs an string for another string)

StrLowcase(Make Str LowerCase)

StrUpcase(Make Str UpperCase)

StrReverse(Make Str Reverse)

StrLeft(Gets a part of string from first char)

StrRight(Gets a part of string from last char)

StrMid(Gets a part of string from an specified char)

MakeHexColor(Gets an string like #0034FF for r,g,b values)

 

Note!

if offset of char is x place of that x+1 .(for example offset of first char is 0)

but these funcs want place of chars and NOT offset of them.

 

you'll need this classes database for use these funcs.

you'll need DK22Pac's NewOpcodes.cleo for use these funcs.

you'll need Cleo4 Library for use these funcs.

jump @InStr_skip
{0AC8: 0@ = allocate_memory_size 7 
set_string 0@ "Helolo" 
0AC8: 1@ = allocate_memory_size 2 
set_string 1@ "e" 
call_scm_func @InStr 3 start_search_from 1 search_in 0@ search_for 1@ store_char_num_to 2@
0ACE: show_formatted_text_box "Founded in %d" 2@}

:InStr
3@ = String.Length(1@)
3@ -= 1
6@ = String.Length(2@)
6@ += 1
7@ = Memory.Alloc(6@)
7@ = Memory.ReadFromOffset(2@, 0, 6@)
4@ = Memory.Alloc(6@)
0@ -= 1 
For 5@ = 0@ To 3@
  4@ = Memory.ReadFromOffset(1@, 5@, 6@)
  If 
  003B:   4@ == 7@  // (int)
  Then
    break
  End
end
Memory.FreeAlloc(7@)
Memory.FreeAlloc(4@)
5@ += 1
ret 1 5@

:InStr_skip
jump @StrLowcase_skip
{0AC8: 0@ = allocate_memory_size 7 
0AD3: format 0@ "Helolo" 
0AB1: call_scm_func @StrLowcase 2 source_string 0@ use_same_buffer 1 store_to 0@
// if use_same_buffer is 1 function will use the same buffer for the reversed string else function will alloc a new buffer for reversed string. 
}

:StrLowcase
2@ = String.Length(0@) // what do you want from me? description? :|
if 
  2@ > 0 // if length is 0 dont ret any value 
else_jump @StrLowcase_336 
2@ += 1
6@ = Memory.Alloc(2@) // alloc a buffer with size of length of source
for 4@ = 1 to 2@
    5@ = Memory.ReadFromOffset(0@, 7@, 1) // read
    if and
    5@ >= 65
    5@ <= 90
    jf @StrLowcase_142
    5@ += 32
    :StrLowcase_142
    Memory.WriteToOffset(6@, 3@, 1, 5@) // write
    7@ += 1 //next char of source string 
    3@ += 1 //next char of detination string 
end
if
1@ == 1
jf @StrLowcase_32X    
set_string 0@ "%s" 6@ // write reversed string in buffer of base string
Memory.FreeAlloc(6@)// free alloced buffer

:StrLowcase_328
ret 1 0@ // :-w 

:StrLowcase_32X
ret 1 6@ // :-w 

:StrLowcase_336
ret 0 

:StrLowcase_skip
jump @StrUpcase_skip
{0AC8: 0@ = allocate_memory_size 7 
0AD3: format 0@ "Helolo" 
0AB1: call_scm_func @StrUpcase 2 source_string 0@ use_same_buffer 1 store_to 0@
// if use_same_buffer is 1 function will use the same buffer for the reversed string else function will alloc a new buffer for reversed string. 
}

:StrUpcase
2@ = String.Length(0@) // what do you want from me? description? :|
if 
  2@ > 0 // if length is 0 dont ret any value 
else_jump @StrUpcase_336
2@ += 1 
6@ = Memory.Alloc(2@) // alloc a buffer with size of length of source
for 4@ = 1 to 2@
5@ = Memory.ReadFromOffset(0@, 7@, 1) // read
if and
5@ >= 97
5@ <= 122
jf @StrUpcase_142
5@ -= 32
:StrUpcase_142
Memory.WriteToOffset(6@, 3@, 1, 5@) // write
7@ += 1 //next char of source string 
3@ += 1 //next char of detination string 
end
if
1@ == 1
jf @StrUpcase_32X    
set_string 0@ "%s" 6@ // write reversed string in buffer of base string
Memory.FreeAlloc(6@)// free alloced buffer

:StrUpcase_328
ret 1 0@ // :-w 

:StrUpcase_32X
ret 1 6@ // :-w 

:StrUpcase_336
ret 0 

:StrUpcase_skip
jump @StrReverse_skip
{0AC8: 0@ = allocate_memory_size 7 
0AD3: format 0@ "Helolo" 
0AB1: call_scm_func @StrReverse 2 source_string 0@ use_same_buffer 1 store_to 0@
// if use_same_buffer is 1 function will use the same buffer for the reversed string else function will alloc a new buffer for reversed string. 
}

:StrReverse
2@ = String.Length(0@) // what do you want from me? description? :|
if 
  2@ > 0 // if length is 0 dont ret any value 
else_jump @StrReverse_336 
if 
  2@ > 1 // reverse of a one character string is that self... eh? 
else_jump @StrReverse_328 
2@ += 1
6@ = Memory.Alloc(2@) // alloc a buffer with size of length of source
0A8F: 3@ = 2@ - 1 // offset of last char 
for 4@ = 1 to 2@
    5@ = Memory.ReadFromOffset(0@, 7@, 1) // read
    Memory.WriteToOffset(6@, 3@, 1, 5@) // write
    7@ += 1 //next char of source string 
    3@ -= 1 //previous char of detination string 
end
if
1@ == 1
jf @StrReverse_32X    
set_string 0@ "%s" 6@ // write reversed string in buffer of base string
Memory.FreeAlloc(6@)// free alloced buffer

:StrReverse_328
ret 1 0@ // :-w 

:StrReverse_32X
ret 1 6@ // :-w 

:StrReverse_336
ret 0 

:StrReverse_skip
jump @StrLeft_skip
{0AC8: 0@ = allocate_memory_size 7 
0AD3: format 0@ "Helolo" 
0AB1: call_scm_func @StrLeft 3 source_string 0@ num_copy_chars 3 use_same_buffer 1 store_to 0@
// if use_same_buffer is 1 function will use the same buffer for the reversed string else function will alloc a new buffer for reversed string. 
}

:StrLeft
3@ = String.Length(0@) // what do you want from me? description? :|
if and 
  3@ > 0 // if length is 0 dont ret any value
  1@ > 0 
else_jump @StrLeft_336 
if
002D:   3@ >= 1@  // (int)
else_jump @StrLeft_328
1@ += 1 
7@ = Memory.Alloc(1@)// alloc a buffer with size of
for 5@ = 1 to 1@
    6@ = Memory.ReadFromOffset(0@, 8@, 1) // read
    Memory.WriteToOffset(7@, 4@, 1, 6@) // write
    8@ += 1 //next char of source string 
    4@ += 1 //next char of detination string 
end
if
2@ == 1
jf @StrLeft_32X    
set_string 0@ "%s" 7@ // write reversed string in buffer of base string
Memory.FreeAlloc(7@)// free alloced buffer

:StrLeft_328
ret 1 0@ // :-w 

:StrLeft_32X
ret 1 7@ // :-w 

:StrLeft_336
ret 0 

:StrLeft_skip
jump @InStr_skip
{0AC8: 0@ = allocate_memory_size 7 
0AD3: format 0@ "Helolo" 
0AB1: call_scm_func @StrRight 3 source_string 0@ num_copy_chars 3 use_same_buffer 1 store_to 0@
// if use_same_buffer is 1 function will use the same buffer for the reversed string else function will alloc a new buffer for reversed string. 
}

:StrRight
3@ = String.Length(0@) // what do you want from me? description? :|
if and 
  3@ > 0 // if length is 0 dont ret any value
  1@ > 0 
else_jump @StrRight_336 
if
002D:   3@ >= 1@  // (int)
else_jump @StrRight_328
1@ += 1 
7@ = Memory.Alloc(1@)// alloc a buffer with size of
0A8F: 8@ = 3@ - 1 // int
0A8F: 4@ = 1@ - 1 // int
for 5@ = 1 to 1@
6@ = Memory.ReadFromOffset(0@, 8@, 1) // read
Memory.WriteToOffset(7@, 4@, 1, 6@) // write
8@ -= 1 //previous char of source string 
4@ -= 1 //previous char of detination string 
end
if
2@ == 1
jf @StrRight_32X    
set_string 0@ "%s" 7@ // write reversed string in buffer of base string
Memory.FreeAlloc(7@)// free alloced buffer

:StrRight_328
ret 1 0@ // :-w 

:StrRight_32X
ret 1 7@ // :-w 

:StrRight_336
ret 0

:InStr_skip 

jump @StrMid_skip
{0AC8: 0@ = allocate_memory_size 7 
0AD3: format 0@ "Helolo" 
0AB1: call_scm_func @StrMid 4 source_string 0@ start_from 1 num_copy_chars 1 use_same_buffer 1 store_to 0@
// if use_same_buffer is 1 function will use the same buffer for the reversed string else function will alloc a new buffer for reversed string. 
}

:StrMid
4@ = String.Length(0@) // what do you want from me? description? :|
if and 
  4@ > 0 // if length is 0 dont ret any value
  2@ > 0 
else_jump @StrMid_336 
if
002D:   4@ >= 2@  // (int)
else_jump @StrMid_328
2@ += 1 
8@ = Memory.Alloc(2@)
0A8F: 9@ = 1@ - 1 // int
for 6@ = 1 to 2@
    7@ = Memory.ReadFromOffset(0@, 9@, 1)
    Memory.WriteToOffset(8@, 5@, 1, 7@)
    9@ += 1 //next char of source string 
    5@ += 1 //previous char of detination string 
end
if
3@ == 1
jf @StrMid_32X    
set_string 0@ "%s" 8@ // write reversed string in buffer of base string
Memory.FreeAlloc(8@)// free alloced buffer

:StrMid_328
ret 1 0@ // :-w 

:StrMid_32X
ret 1 8@ // :-w 

:StrMid_336
ret 0

:StrMid_skip
{ 
0AB1: call_scm_func @MakeHexColor 3 255 128 0 store_to 0@ // result will be => "#FF8000"
//returns a string
}
:MakeHexColor
3@ = Memory.Alloc(2)
4@ = Memory.Alloc(8)
0C0D: struct 4@ offset 0 size 1 = "#"
5@ = Memory.Alloc(17)
0C0D: struct 5@ offset 0 size 16 = "0123456789ABCDEF"
// 0@
0B14: 6@ = 0@ MOD 16
0A8F: 7@ = 0@ - 6@ // int
7@ /= 16
0AB1: call_scm_func @StrMid 4 source_string 5@ start_from 7@ num_copy_chars 1 use_same_buffer 0 store_to 3@
0D4A: concatenate_strings 4@ 3@
0AB1: call_scm_func @StrMid 4 source_string 5@ start_from 6@ num_copy_chars 1 use_same_buffer 0 store_to 3@
0D4A: concatenate_strings 4@ 3@
// 1@
0B14: 6@ = 1@ MOD 16
0A8F: 7@ = 1@ - 6@ // int
7@ /= 16
0AB1: call_scm_func @StrMid 4 source_string 5@ start_from 7@ num_copy_chars 1 use_same_buffer 0 store_to 3@
0D4A: concatenate_strings 4@ 3@
0AB1: call_scm_func @StrMid 4 source_string 5@ start_from 6@ num_copy_chars 1 use_same_buffer 0 store_to 3@
0D4A: concatenate_strings 4@ 3@
// 2@
0B14: 6@ = 2@ MOD 16
0A8F: 7@ = 2@ - 6@ // int
7@ /= 16
0AB1: call_scm_func @StrMid 4 source_string 5@ start_from 7@ num_copy_chars 1 use_same_buffer 0 store_to 3@
0D4A: concatenate_strings 4@ 3@
0AB1: call_scm_func @StrMid 4 source_string 5@ start_from 6@ num_copy_chars 1 use_same_buffer 0 store_to 3@
0D4A: concatenate_strings 4@ 3@
Memory.FreeAlloc(3@)
Memory.FreeAlloc(5@)
ret 1 4@

:StrMid
4@ = String.Length(0@) // what do you want from me? description? :|
if and 
  4@ > 0 // if length is 0 dont ret any value
  2@ > 0 
else_jump @StrMid_336 
if
002D:   4@ >= 2@  // (int)
else_jump @StrMid_328
2@ += 1 
8@ = Memory.Alloc(2@)
0085: 9@ = 1@ // (int)
for 6@ = 1 to 2@
    7@ = Memory.ReadFromOffset(0@, 9@, 1)
    Memory.WriteToOffset(8@, 5@, 1, 7@)
    9@ += 1 //next char of source string 
    5@ += 1 //previous char of detination string 
end
if
3@ == 1
jf @StrMid_32X    
set_string 0@ "%s" 8@ // write reversed string in buffer of base string
Memory.FreeAlloc(8@)// free alloced buffer

:StrMid_328
ret 1 0@ // :-w 

:StrMid_32X
ret 1 8@ // :-w 

:StrMid_336
ret 0

fastman92
  • fastman92

    фастман92 | ف

  • Members
  • Joined: 28 Jul 2009
  • None
  • Contribution Award [Mods]

#2

Posted 01 January 2014 - 11:38 PM

"Helolo" as C string requires 7 bytes.

h
e
l
o
l
o
(NULL, byte with value zero)

Functions of programs made in C or C++
work with this kind of string.
Your code has an undefined behavior and this memory access violation may result in a game crash.

MasterHK
  • MasterHK

    کیان حامی‏

  • Members
  • Joined: 20 Mar 2013
  • Iran

#3

Posted 02 January 2014 - 03:17 AM

but in my tests it wok good!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users