# AVSLib's temporary LeftStr, RightStr, MidStr replacements up to Avisynth Version 2.55
# Copyright (c) 2005 George Zarkadas (gzarkadas@users.sourceforge.net)

# This program is free software; you can redistribute it and/or modify it under the terms of 
# the GNU General Public License as published by the Free Software Foundation; either 
# version 2 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
# PURPOSE.  See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program; 
# if not, write to the "Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
# MA  02111-1307  USA"


# THE INFO BELOW IS TRUE UP TO AVISYNTH VERSION 2.55 
# Versions 2.56 and later allow **much** more memory for strings

# Hack to double max string size of arrays to 1K.
# LeftStr, RightStr, MidStr have an internal buffer of ~1k where both the string passed 
# as argument and the return value are stored. Thus if the string wanted differs only 
# a few chars from the argument (such as in LeftStr(array, StrLen(array) - __ARRAY_LEN) 
# then VDub dies silently due to buffer overflow in Avisynth for array strings of ~0.5k
# In addition, there is a ~4K limit for strings returned from user functions  [which also leads to buffer overflow and silent death of VDub after the first 
# string with length > 4103 is returned succesfully and the script returns a clip (for example when you request the open dialog box)]

# All functions divide s[slen] to s1[cut] + s2[slen-cut]

Function __left_str(string s, int length)
{
	slen = StrLen(s)
	Assert(length >= 0, "__left_str: Negative character count not allowed")
	Assert(slen <= 1024, "__left_str: String has > 1024 chars")
	cut = Int(slen / 2)
	p1 = LeftStr(s, __min2(length, cut))
	p2 = MidStr(s, cut + 1, __max2(length - cut, 0))
	return p1 + p2
}

Function __right_str(string s, int length)
{
	slen = StrLen(s)
	Assert(length >= 0, "__right_str: Negative character count not allowed")
	Assert(slen <= 1024, "__right_str: String has > 1024 chars")
	cut = Int(slen / 2)
	p1 = MidStr(s, __max2(slen - length, 0) + 1, __min2(__max2(cut - slen + length, 0), cut))
	p2 = RightStr(s, __min2(length, slen - cut))
	return p1 + p2
}

Function __mid_str(string s, int start, int "length")
{
	slen = StrLen(s)
	length = Default(length, slen)
	Assert(start > 0, "__mid_str: Illegal character location") # same error string as std MidStr
	Assert(length >= 0, "__mid_str: Illegal character count") # same error string as std MidStr
	Assert(slen <= 1024, "__mid_str: String has > 1024 chars")
	cut = Int(slen / 2)
	p1 = MidStr(s, start, __max2(__min2(length, cut + 1 - start), 0))
	p2 = MidStr(s, __max2(start, cut + 1), __max2(__min2(length, length - cut - 1 + start), 0))
	return p1 + p2
}

# Public variants
Function StrLeft(string s, int length) { return __left_str(s, length) }
Function StrRight(string s, int length) { return __right_str(s, length) }
Function StrMid(string s, int start, int "length") { return __mid_str(s, start, length) }
