• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Text Parser Snippet [v.1.1]

This bundle is marked as useful / simple. Simplicity is bliss, low effort and/or may contain minor bugs.
Text Parsing Snippet.

This system allows you to take any length of code, and break it up depending on the length you want. It also wraps words so they don't look broken.

Very useful for multiboards and game messages.


JASS:
//**********************************************************************
// Forsakener's Text Parsing Snippet
//
// To Use:
// Copy and Paste this system into your map.
// 
// Please give credit if you use this.
//**********************************************************************
library TextParsing

globals
    private integer array Formula[4]
    private string array Lines[50]
    private integer MaxLines = 0
endglobals

function ParseText takes string input, integer max returns nothing
    local integer length = StringLength(input)
    local integer lines = (length/max)+1
    local integer i = 1
    local integer b = 1
    local integer prewrap = 0
    local integer wrap = 0
    local integer array Space
    local integer SpaceLoc = 0
    set MaxLines = lines
    
    loop
        exitwhen i > lines
        
        set Lines[i] = ""
        set Formula[1] = (i*max)
        set prewrap = wrap
        set Formula[2] = ((max-1)-prewrap)
        set Formula[3] = ((i*max)-prewrap)
        set Formula[4] = ((Formula[3]-Formula[2])-prewrap)
        set b = Formula[4]
        set wrap = 0
        set SpaceLoc = 0
        
        loop
            exitwhen b > Formula[1]
            
            if SubString(input, b-1, b) == " " then
                set SpaceLoc = SpaceLoc+1
                set Space[SpaceLoc] = b
            endif
            set b = b+1
            
        endloop
        
        if (SubString(input, Formula[1]-1, Formula[1]) != " ") and (SubString(input, (Formula[1]+1)-1, Formula[1]+1) != " ") and (StringLength(SubString(input, Formula[4]-1, Formula[1])) >= (max-prewrap)) then
            set wrap = wrap+((i*max)-Space[SpaceLoc])
            set Formula[1] = (i*max)-wrap
            set Lines[i] = SubString(input, Formula[4]-1, Formula[1])
        else
            set Lines[i] = SubString(input, Formula[4]-1, Formula[1])
        endif
        set i = i+1
        
    endloop
    
endfunction

function GetMaxLines takes nothing returns integer
    return MaxLines
endfunction

function GetLine takes integer line returns string 
    return Lines[line]
endfunction


endlibrary


The functions to use for this snippet are:

JASS:
function ParseText takes string input, integer max returns nothing
This is the initial function to start the system. Simply:
call ParseText("TEXT", MAX)

JASS:
function GetMaxLines takes nothing returns integer m
This is the function that gets the amount of lines the output contains. Use this for loops

JASS:
function GetLine takes integer line returns string l
This function goes with the loop with GetMaxLines(). This receives the output of the system.


v1.1 - Removed unneeded code in functions: GetLine; GetMaxLines


Keywords:
text, parse, paragraph, snippet, system, apples, cucumbers, obama, iraq, ralle, forsakener
Contents

Just another Warcraft III map (Map)

Reviews
12th Dec 2015 IcemanBo: Too long time as NeedsFix. Rejected. 11:19, 9th Feb 2011 Bribe: See the post I wrote for you and Nestharus' descriptions

Moderator

M

Moderator

12th Dec 2015
IcemanBo: Too long time as NeedsFix. Rejected.

11:19, 9th Feb 2011
Bribe: See the post I wrote for you and Nestharus' descriptions
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
This could easily just return one string by inputting \n throughout it.

I don't see a need to be able to access specific lines. If you need to access specific lines, could still be done in one function by returning a stack.

Furthermore, this seems to do word wrapping. You should rename it for more clarity. String parsing could be anything to do with parsing, so this is pretty vague and ambiguous.

edit
Wrote this out
JASS:
library WrapString
    function WrapString takes string stringToWrap, integer maxLineLength returns string
        local integer i = 1
        local integer length = StringLength(stringToWrap)
        local integer space = 0
        local string new = ""
        local integer last = 0
        local integer cur = 0
        loop
            set cur = cur + 1
            if ((SubString(stringToWrap, i-1, i)) == " ") then
                if (i-1 == last) then
                    set cur = cur - 1
                else
                    set space = i
                endif
            endif
            if (i == length) then
                if (new != "") then
                    set new = new + "\n"
                endif
                return new + SubString(stringToWrap, last, i)
            elseif (cur >= maxLineLength and space > last) then
                if (new != "") then
                    set new = new + "\n"
                endif
                set new = new + SubString(stringToWrap, last, space)
                set last = space
                set cur = 0
            endif
            set i = i + 1
        endloop
        
        return new
    endfunction
endlibrary

and demo
JASS:
private struct test extends array
    private static method tests takes nothing returns boolean
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 100000, WrapString(GetEventPlayerChatString(), 9))
        return false
    endmethod
    
    private static method onInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerAddCondition(t, Condition(function thistype.tests))
        call TriggerRegisterPlayerChatEvent(t, Player(0), "", false)
    endmethod
endstruct


The above essentially has the same output as yours without the nested loops, extra globals, 2 extra functions, and array locals : P

I say essentially because this one cuts off the first space of a new line if that line begins with a space. In your screenshot, the last line starts with a space. With this code, the last line wouldn't start with a space ; ).

Feel free to use the above code in your lib = P.
 
Last edited:
Can you write this in Jass or is Jass too difficult for you? Because there's no need for vJass in this function.

I don't know why NOT to do this in vJass

In vJass you can make most things shorter and more efficient than in Jass or GUI.

How many mappers (which are making maps and not learning how to map) do you know which do not have the JNGP?
 
@OffGraphic - if you want the systems in GUI or JASS why not do them yourself rather than going through the resources and blabbing about, this is doable in GUI, write this in JASS, etc...

+I like vJASS systems because you can just copy them into a blank trigger rather than copying a JASS system into the map header (which could lead to a harder readability when you import a lot of systems)...

anyway, 3/5 :goblin_good_job:
 
Consider the recommendations from Nestharus, if anyone knows how to do string parsing, he does. I have to agree the name itself is just not descriptive for what this does. Nestharus wrote StringParser, take a look at his library (in the JASS Forum) and you will see THAT is dynamic string parsing, worthy of its name, and this is definitely a snippet that provides only a single type of parsing.
 
Top