Difference between revisions of "George Procedures : Basic"

From TVPaintWiki
Jump to: navigation, search
Line 39: Line 39:
 
RETURN 0
 
RETURN 0
 
END
 
END
</nowiki>
+
</nowiki>
 
== FirstPos(string,car) ==
 
== FirstPos(string,car) ==
  

Revision as of 09:31, 26 November 2013

Find(string,car,start)

//------------------------------------------ // // Find // // Function: find a character into a string // from a specified start position // // Call: Find(string,car,start) // // Arguments: // string = characters string // car = character to look for // start = start position in string // // Return: position of character // 0 if character does not exist // into string // -1 if invalid position // (negative, null or higher // than string length) // //------------------------------------------

Insert non-formatted text here FUNCTION Find(string,car,start) LOCAL i size i = start size = LEN(string) IF ((start <= 0) || (start > size) || (CMP(car,"") == 1)) RETURN -1 END DO IF (CMP(CHAR(string,i),car) == 1) RETURN i END UNTIL ((i=i+1) > size) RETURN 0 END

FirstPos(string,car)

//------------------------------------------ // // FirstPos // // Function: find first occurence of a // character into a string // // Call: FirstPos(string,car) // // Arguments: // string = characters string // car = character to look for // // Return: position of character // or 0 id character does not // exist into string. // //------------------------------------------

FUNCTION FirstPos(string,car) RETURN Find(string,car,1) END //}}}

//{{{ LastPos(string,car) //------------------------------------------ // // LastPos // // Function: find last occurence of a // character into a string // // Call: LastPos(string,car) // // Arguments: // string = characters string // car = character to look for // // Return: position of character // or 0 if character does not // exist into string. // //------------------------------------------

FUNCTION LastPos(string,car) LOCAL loop myChar pos = LEN(string) loop = 1 WHILE loop myChar = CHAR(string,pos)

IF CMP(myChar,car) == 1 loop =0 ELSE pos = pos-1 END IF pos<1 loop=0 END END RETURN pos END //}}}

FindString(string,search,start)

//------------------------------------------ // // FindString // // Function: find a substring into a string // from a specified start position // // Call: FindString(string,search,start) // // Arguments: // string = characters string // search = substring to look for // start = start position in string // // Return: start position of substring // 0 if substring does not exist // into string // -1 if invalid start position // (negative, null or higher // than string length) or if // substring is empty // //------------------------------------------

FUNCTION FindString(string,search,start) LOCAL found j pos lastpos lstr lsrch found = 0 lastpos = start lstr = LEN(string) lsrch = LEN(search)

IF (CMP(search,"") == 1) RETURN -1 END WHILE ((pos = Find(string,CHAR(search,1),lastpos)) > 0) j = 1 found = 1 IF (j == lsrch) RETURN pos END WHILE ((j < lsrch) && (found == 1)) IF ((pos+j) > lstr) RETURN 0 ELSE IF (CMP(CHAR(search,j+1),CHAR(string,pos+j)) == 0) lastpos = pos+j found = 0 END END j = j+1 END IF (found == 1) RETURN pos END END RETURN pos END


LeftString(string,number)

//------------------------------------------ // // LeftString // // Function: extract a substring beginning // at the start of a string // // Call: LeftString(string,number) // // Arguments: // string = characters string // number = number of character to // extract from string // // Return: result substring // or 0 if number is negative // or null. // //------------------------------------------

FUNCTION LeftString(string,number) LOCAL size size = LEN(string)

IF (number > 0) IF (number > size) number = size END RETURN CUT(string,1,number) END RETURN 0 END


RightString(string,number)

//------------------------------------------ // // RightString // // Function: extract a substring ending // at the end of a string // // Call: RightString(string,number) // // Arguments: // string = characters string // number = number of character to // extract from string // // Return: result substring // or 0 if number is negative // or null. // //------------------------------------------

FUNCTION RightString(string,number) LOCAL size size = LEN(string)

IF (number > 0) IF (number > size) number = size END RETURN CUT(string,size-number+1,size) END RETURN 0 END


MidString(string,first,size)

//------------------------------------------ // // MidString // // Function: extract a substring from a // string // // Call: MidString(string,first,size) // // Arguments: // string = characters string // first = start position in string // cha�ne // size = number of characters // // Return: result substring // or 0 if number or first are // negatives or null. // //------------------------------------------

FUNCTION MidString(string,first,size) LOCAL ln ln = LEN(string)

IF ((first > 0) && (size > 0)) IF ((first+size-1) > ln) size = ln-first+1 END RETURN CUT(string,first,first+size-1) END RETURN 0 END //}}}

InsertAtPos(string,insert,pos)

//------------------------------------------ // // InsertAtPos // // Function: insert a string in another // string at a specified position // // Call: InsertAtPos(string,insert,pos) // // Arguments: // string = characters string // insert = string to insert // pos = start position for insertion // // Return: result string after insertion // or 0 if start position is // invalid (negative, null or // higher than string length) // //------------------------------------------

FUNCTION InsertAtPos(string,insert,pos) LOCAL workstr str1 size size = LEN(string)

IF ((pos < 0) || (pos > size)) RETURN 0 END IF (pos == 0) RETURN insert""string END IF (pos == size) RETURN string""insert END workstr = CUT(string,1,pos) IF (pos < size) str1 = CUT(string,pos+1,size) RETURN workstr""insert""str1 ELSE RETURN workstr""insert END END