- Joined
- Nov 7, 2014
- Messages
- 571
Strings in Jass are immutable and there really isn't much support for working with them. This is an example implementation of mutable strings that are somewhat easier to work with.
JASS:
library mutstrdemo initializer mutstr_demo uses libmutstr, libH
function im_ascii_to_lower takes H_u8 s returns nothing
call ascii_to_lower(s)
call s.destroy() // s is an immediate
endfunction
function im_ascii_to_upper takes H_u8 s returns nothing
call ascii_to_upper(s)
call s.destroy()
endfunction
globals
mutstr g_s
boolean dir_is_up
integer color_offset
integer letters_offset
endglobals
function prev_hex_digit takes integer d returns integer
local integer p
if d == '0' then
set p = 'F'
elseif d == '1' then
set p = '0'
elseif d == '2' then
set p = '1'
elseif d == '3' then
set p = '2'
elseif d == '4' then
set p = '3'
elseif d == '5' then
set p = '4'
elseif d == '6' then
set p = '5'
elseif d == '7' then
set p = '6'
elseif d == '8' then
set p = '7'
elseif d == '9' then
set p = '8'
elseif d == 'A' then
set p = '9'
elseif d == 'B' then
set p = 'A'
elseif d == 'C' then
set p = 'B'
elseif d == 'D' then
set p = 'C'
elseif d == 'E' then
set p = 'D'
elseif d == 'F' then
set p = 'E'
else
call assert("[prev_hex_digit]: d = " + I2S(d) + " is not a hex digit", false)
endif
return p
endfunction
function next_hex_digit takes integer d returns integer
local integer p
if d == '0' then
set p = '1'
elseif d == '1' then
set p = '2'
elseif d == '2' then
set p = '3'
elseif d == '3' then
set p = '4'
elseif d == '4' then
set p = '5'
elseif d == '5' then
set p = '6'
elseif d == '6' then
set p = '7'
elseif d == '7' then
set p = '8'
elseif d == '8' then
set p = '9'
elseif d == '9' then
set p = 'A'
elseif d == 'A' then
set p = 'B'
elseif d == 'B' then
set p = 'C'
elseif d == 'C' then
set p = 'D'
elseif d == 'D' then
set p = 'E'
elseif d == 'E' then
set p = 'F'
elseif d == 'F' then
set p = '0'
else
call assert("[next_hex_digit]: d = " + I2S(d) + " is not a hex digit", false)
endif
return p
endfunction
function im_next_hex_digit_H_u8 takes H_u8 s returns nothing
local integer i = 0
local integer ii = s.len
loop
exitwhen i >= ii
set s[i] = next_hex_digit(s[i])
set i = i + 1
endloop
call s.destroy() // s is an immediate
endfunction
function im_prev_hex_digit_H_u8 takes H_u8 s returns nothing
local integer i = 0
local integer ii = s.len
loop
exitwhen i >= ii
set s[i] = prev_hex_digit(s[i])
set i = i + 1
endloop
call s.destroy()
endfunction
function print_mutstr takes nothing returns nothing
call ClearTextMessages()
call BJDebugMsg(g_s.str())
if g_s[color_offset] == 'F' then
set dir_is_up = false
elseif g_s[color_offset] == '0' then
set dir_is_up = true
endif
if dir_is_up then
call im_next_hex_digit_H_u8(g_s.buf.H(color_offset, color_offset + 6))
set g_s.dirty = true
call im_ascii_to_upper(g_s.buf.H(letters_offset, letters_offset + 26))
else
call im_prev_hex_digit_H_u8(g_s.buf.H(color_offset, color_offset + 6))
set g_s.dirty = true
call im_ascii_to_lower(g_s.buf.H(letters_offset, letters_offset + 26))
endif
endfunction
function mutstr_demo takes nothing returns nothing
local integer i
set g_s = mutstr.create()
call g_s.push2(0xCE, 0xB1)
call g_s.push_string(">count leaves\n")
call g_s.push_string("|cff")
set color_offset = g_s.len
call g_s.push_string("000000There are 69105 leaves here.|r\n")
set i = 0x91
loop
exitwhen i > 0xA9
call g_s.push2(0xCE, i)
set i = i + 1
endloop
set i = 0xB1
loop
exitwhen i > 0xBF
call g_s.push2(0xCE, i)
set i = i + 1
endloop
set i = 0x80
loop
exitwhen i > 0x89
call g_s.push2(0xCF, i)
set i = i + 1
endloop
call g_s.push(10)
set letters_offset = g_s.len
call g_s.push_string("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n")
set dir_is_up = true
call TimerStart(CreateTimer(), 0.1, true, function print_mutstr)
endfunction
endlibrary
Attachments
Last edited: