1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. Don’t forget to sign up for the Hive Cup. There’s a 555 EUR prize pool. Sign up now!
    Dismiss Notice
  4. The Hive Workshop Cup contest results have been announced! See the maps that'll be featured in the Hive Workshop Cup tournament!
    Dismiss Notice
  5. Units have been turned into heroes in our latest Icon Contest! Vote for the best icon set at the Icon Contest #18 Poll!
    Dismiss Notice
  6. The poll for Hive's 12th Concept Art Contest is up! Go cast your vote for your favourite genie!
    Dismiss Notice
  7. The raddest synthwave tracks were chosen - Check out our Music Contest #12 - Results and congratulate the winners!
    Dismiss Notice
  8. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[Snippet] RemoveStringSpaces

Discussion in 'JASS Resources' started by Magtheridon96, Dec 8, 2012.

  1. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    FileIONameSafety was a pretty silly library with very limited application.
    I decided to get rid of it and replace with something more modular and general-purpose.

    This library is used to remove the leading and trailing spaces in a string, and it can be used to do what FileIONameSafety did just by doing the following:

    Example
    Code (vJASS):
    function FixPlayerName takes player p returns nothing
        call SetPlayerName(p, RemoveLeadingSpaces(GetPlayerName(p)))
    endfunction


    Code

    Code (vJASS):
    /********************************
    *
    *   RemoveStringCharacters
    *   v1.1.0.0
    *   By Magtheridon96
    *
    *   - This library allows you to remove
    *     leading and trailing spaces from a string
    *     and it ensures minimal string leaks in
    *     doing so.
    *
    *   API:
    *   ----
    *
    *       - function RemoveLeadingCharacters takes string whichString, string character returns string
    *           - Removes all leading instances of character in whichString.
    *       - function RemoveLeadingSpaces takes string whichString returns string
    *           - Removes all leading spaces in whichString.
    *       - function RemoveTrailingCharacters takes string whichString, string character returns string
    *           - Removes all trailing instances of character in whichString.
    *       - function RemoveTrailingSpaces takes string whichString returns string
    *           - Removes all trailing spaces in whichString.
    *       - function StringTrimCharacters takes string whichString, string character returns string
    *           - Removes all trailing and leading instances of character in whichString.
    *       - function StringTrim takes string whichString returns string
    *           - Removes all trailing and leading spaces in whichString.
    *
    ********************************/

    library RemoveStringCharacters

        function RemoveLeadingCharacters takes string str, string character returns string
            local integer position = 0
            local integer length = StringLength(str)
           
            loop
                exitwhen position == length or SubString(str, position, position + 1) != character
                set position = position + 1
            endloop
           
            return SubString(str, position, length)
        endfunction
       
        function RemoveLeadingSpaces takes string str returns string
            return RemoveLeadingCharacters(str, " ")
        endfunction

        function RemoveTrailingCharacters takes string str, string character returns string
            local integer length = StringLength(str)
            local integer position = length - 1

            loop
                exitwhen position == -1 or SubString(str, position, position + 1) != character
                set position = position - 1
            endloop

            return SubString(str, 0, position + 1)
        endfunction
       
        function RemoveTrailingSpaces takes string str returns string
            return RemoveTrailingCharacters(str, " ")
        endfunction

        function StringTrimCharacters takes string whichString, string character returns string
            return RemoveTrailingCharacters(RemoveLeadingCharacters(whichString, character), character)
        endfunction

        function StringTrim takes string whichString returns string
            return StringTrimCharacters(whichString, " ")
        endfunction
       
    endlibrary

    // Backwards compatibility
    library RemoveStringSpaces requires RemoveStringCharacters
    endlibrary


    Feel free to comment.
     
    Last edited: Jan 13, 2014
  2. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,149
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    Good = )
     
  3. Ty3uK

    Ty3uK

    Joined:
    Sep 12, 2011
    Messages:
    46
    Resources:
    1
    Spells:
    1
    Resources:
    1
    I've wrote "C# string class" with Trim method :p
    But it's codded on cJASS, because it's very cool stuff i needed method overloading. :)
    Here code
    Code (vJASS):
    /**************************************
    *
    *   String Class
    *       v1.0
    *   By Max Karelov aka Ty3uK
    *
    *   -Struct such as "string" class in C#
    *
    *   Requirements:
    *   -------------
    *
    *        Jass New Gen Pack 5d with latest AdicHelper and JassHelper
    *            Here is download link:
    *                [url]http://www.mediafire.com/?aqfwy8wmrcuf67k[/url]
    *
    *   API:
    *   ----
    *
    *        Methods:
    *            Clone() - returns copy of current copy of String.
    *            Equals(String toCompare) - compare current copy of String with argumented copy of String. Returns boolean.
    *            GetHashCode() - returns hash of current copy of String.
    *            IndexOf(string value) - returns first index of argumented value.
    *            IndexOf(string value, int start) - returns first index of argumented value since start position.
    *            Insert(int start, string value) - returns string with inserted value on a start position.
    *            LastIndexOf(string value) - returns last index of argumented value.
    *            LastIndexOf(string value, int start) - returns last index of argumented value since start position.
    *            Remove(int start) - removes all symbols after start position. Returns string.
    *            Remove(int start, int count) - removes defined count of symbols after start position. Returns string.
    *            Replace(string old, string new) - replacing all old values with new values. Returns string.
    *            Substring(int start) - returns substring of current copy of String after start position.
    *            Substring(int start, int length) - returns substring of current copy of String after start position with defined length.
    *            Trim() - removes all spaces chars before and after text of current copy of String. Returns string.
    *            create(string value) - create new copy of String with defined start value;
    *            destroy() - destroy current copy of String.
    *
    *        Operators:
    *            [index] - returns indexed char of current copy of String.
    *            [index]= - sets defined char of current copy of String.
    *            Text - returns text of current copy of String.
    *            Text = - sets new value of current copy of String.
    *
    *   Cheers:
    *   ----
    *        SirNikolas
    *
    **************************************/

    library_once StringClass {
         #include "cj_types_priv.j";
         
         struct String {
             private string VALUE;
             readonly int Length;

             #define {
                 IndexOf(value) = indexOf(value, 0);
                 IndexOf(value, start) = indexOf(value, start);
                 
                 LastIndexOf(value) = lastIndexOf(value);
                 LastIndexOf(value, start) = lastIndexOf_Overload(value, start);
                 
                 Remove(start) = remove(start);
                 Remove(start, count) = remove_Overload(start, count);
                 
                 Substring(start) = substring(start);
                 Substring(start, Length) = substring_Overload(start, Length);
             }

             public string operator [] (int index) {
                return SubString(VALUE, index, index + 1);
             }
             
             public void operator []= (int index, string char) {
                 if (StringLength(char) == 1) {
                     VALUE = SubString(VALUE, 0, index) + char + SubString(VALUE, index + 1, Length);
                 }
             }

             public thistype Clone() {
                 return thistype.create(VALUE);
             }
             
             public bool Equals (thistype toCompare) {
                 return (Text == toCompare.Text);
             }
             
             public int GetHashCode() {
                 return StringHash(VALUE);
             }
             
             public int indexOf(string value, int start) {
                 int length = StringLength(value);
                 whilenot (start >= Length) {
                     if (SubString(VALUE, start, start + length) == value) { return start + length; }
                     start++;
                 }
                 return -1;
             }
             
             public string Insert(int start, string value) {
                 return SubString(VALUE, 0, start) + value + SubString(VALUE, start, Length);
             }
             
             public int lastIndexOf(string value) {
                 int length = StringLength(value);
                 for (int i = Length; i > 0; i--) {
                     if (SubString(VALUE, i - length, i) == value) { return i; }
                 }
                 return -1;
             }
             
             public int lastIndexOf_Overload(string value, int start) {
                 int Length = StringLength(value);
                 for (int i = start; i > 0; i--) {
                     if (SubString(VALUE, i - Length, i) == value) { return i; }
                 }
                 return -1;
             }
             
             public string remove(int start) {
                 return SubString(VALUE, 0, start);
             }
             
             public string remove_Overload(int start, int count) {
                 return SubString(VALUE, 0, start) + SubString(VALUE, start + count, Length);
             }
             
             public string Replace(string old, string new) {
                 int oldLength = StringLength(old);
                 int newLength = StringLength(new);
                 int Length = Length;
                 string out = VALUE;
                 for (int i = 0; i < Length; i++) {
                     if (SubString(out, i, i + oldLength) == old) {
                         out = SubString(out, 0, i) + new + SubString(out, i + oldLength, Length);
                         if (oldLength != newLength) { Length = StringLength(out); }
                     } elseif ((Length - i) < oldLength) {
                         break;
                     }
                 }
                 return out;
             }
             
             public string operator Text() {
                 return VALUE;
             }
             
             public void operator Text=(string value) {
                 VALUE = value;
                 Length = StringLength(value);
             }
             
             public string substring(int start) {
                 return SubString(VALUE, start, Length);
             }
             
             public string substring_Overload(int start, int Length) {
                 return SubString(VALUE, start, start + Length);
             }
             
             
             public string Trim() {
                 string out = VALUE;
                 int start = 0, end = Length;
                 string temp;
                 loop {
                     temp = SubString(out, start, start + 1);
                     exitwhen ((temp != " ") && (temp != "\r") && (temp != "\n"));
                     start++;
                 }

                 loop {
                     temp = SubString(out, end - 1, end);
                     exitwhen ((temp != " ") && (temp != "\r") && (temp != "\n"));
                     end--;
                 }
                 if (start > end) { return ""; }
                 return SubString(VALUE, start, end);
             }

             public static thistype create(string value) {
                 thistype out = thistype.allocate();
                 out.VALUE = value;
                 out.Length = StringLength(value);
                 return out;
             }

         }    
    }


    Btw, sorry my eng.
     
    Last edited: Dec 31, 2012
  4. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,861
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
    How about vertical tab?
     
  5. TriggerHappy

    TriggerHappy

    Code Moderator

    Joined:
    Jun 23, 2007
    Messages:
    3,661
    Resources:
    22
    Spells:
    11
    Tutorials:
    2
    JASS:
    9
    Resources:
    22
    I think the API should be
    StringTrim
     
  6. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,861
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
    Cool, I named that function to my Lua local function for my Language lexer XD
     
  7. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    Updated because yeah.
     
  8. TriggerHappy

    TriggerHappy

    Code Moderator

    Joined:
    Jun 23, 2007
    Messages:
    3,661
    Resources:
    22
    Spells:
    11
    Tutorials:
    2
    JASS:
    9
    Resources:
    22
    Code (vJASS):

    function StringTrim takes string whichString returns string
    function StringLTrim takes string whichString returns string
    function StringRTrim takes string whichString returns string
     


    or

    Code (vJASS):

    function StringTrim takes string whichString returns string
    function StringTrimStart takes string whichString returns string
    function StringTrimEnd takes string whichString returns string
     
     
  9. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    The current API is more expressive :v