'string.sbp
'文字列変数の操作用

Function StrPtr(s As String) As *Char
	If Not ActiveBasic.IsNothing(s) Then
		StrPtr = s.StrPtr
	End If
End Function
'StringBuilder版はClasses/System/Text/StringBuilder.abに定義されている

Function ZeroString(length As Long) As System.Text.StringBuilder
	ZeroString = New System.Text.StringBuilder
	ZeroString.Length = length
End Function

Function MakeStr(psz As PSTR) As String
	Return New String(psz)
End Function

Function MakeStr(psz As PWSTR) As String
	Return New String(psz)
End Function

Dim _System_AllocForConvertedString As *Function(size As SIZE_T) As VoidPtr
_System_AllocForConvertedString = AddressOf (GC_malloc_atomic)

Namespace Detail
	Function GetWCStr(mbsSrc As PSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
		Dim lenWCS = MultiByteToWideChar(CP_THREAD_ACP, 0, mbsSrc, (len As DWord) As Long, 0, 0)
		wcsDst = _System_AllocForConvertedString(SizeOf (WCHAR) * (lenWCS + 1)) As PWSTR
		GetWCStr = MultiByteToWideChar(CP_THREAD_ACP, 0, mbsSrc, (len As DWord) As Long, wcsDst, lenWCS)
		wcsDst[GetWCStr] = 0
	End Function

	Function GetWCStr(wcsSrc As PWSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
		wcsDst = wcsSrc
		GetWCStr = len
	End Function

	Function GetMBStr(wcsSrc As PWSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
		Dim lenMBS = WideCharToMultiByte(CP_THREAD_ACP, 0, wcsSrc, (len As DWord) As Long, 0, 0, 0, 0)
		mbsDst = _System_AllocForConvertedString(SizeOf (SByte) * (lenMBS + 1)) As PSTR
		GetMBStr = WideCharToMultiByte(CP_THREAD_ACP, 0, wcsSrc, (wcsSrc As DWord) As Long, mbsDst, lenMBS, 0, 0) As SIZE_T
		mbsDst[GetMBStr] = 0
	End Function

	Function GetMBStr(mbsSrc As PSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
		mbsDst = mbsSrc
		GetMBStr = len
	End Function
End Namespace

/*
変換の組み合わせは、
入力引数: wcsz, wcs + len, mbsz, mbs + len, str
出力関数: wcs(z)出力GetStr, mbs(z)出力GetStr,
          wcs(z)出力GetStrNT, mbs(z)出力GetStrNT,
          GetWCStr, GetMBStr, GetTCStr,
          ToWCStr, ToMBStr, ToTCStr,
で、5 * 10 = 50通り。
*/

Function GetStr(mbszSrc As PSTR, ByRef wcsDst As PWSTR) As SIZE_T
	If mbszSrc = 0 Then
		wcsDst = 0
		Return 0
	Else
		Return Detail.GetWCStr(mbszSrc, lstrlenA(mbszSrc) As SIZE_T, wcsDst)
	End If
End Function

Function GetStr(mbsSrc As PSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
	If mbsSrc = 0 Then
		wcsDst = 0
		Return 0
	Else
		Return Detail.GetWCStr(mbsSrc, len, wcsDst)
	End If
End Function

Function GetStr(wcszSrc As PWSTR, ByRef wcsDst As PWSTR) As SIZE_T
	If wcszSrc = 0 Then
		wcsDst = 0
		Return 0
	Else
		wcsDst = wcszSrc
		Return lstrlenW(wcszSrc) As SIZE_T
	End If
End Function

Function GetStr(wcsSrc As PWSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
	If wcsSrc = 0 Then
		wcsDst = 0
		Return 0
	Else
		wcsDst = wcsSrc
		Return len
	End If
End Function

Function GetStr(wcszSrc As PWSTR, ByRef mbsDst As PSTR) As SIZE_T
	If wcszSrc = 0 Then
		mbsDst = 0
		Return 0
	Else
		Return Detail.GetMBStr(wcszSrc, lstrlenW(wcszSrc) As SIZE_T, mbsDst)
	End If
End Function

Function GetStr(wcsSrc As PWSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
	If wcsSrc = 0 Then
		mbsDst = 0
		Return 0
	Else
		Return Detail.GetMBStr(wcsSrc, len As SIZE_T, mbsDst)
	End If
End Function

Function GetStr(mbszSrc As PSTR, ByRef mbsDst As PSTR) As SIZE_T
	If mbszSrc = 0 Then
		mbsDst = 0
		Return 0
	Else
		mbsDst = mbszSrc
		Return lstrlenA(mbszSrc) As SIZE_T
	End If
End Function

Function GetStr(mbsSrc As PSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
	If mbsSrc = 0 Then
		mbsDst = 0
		Return len
	Else
		mbsDst = mbsSrc
		Return 0
	End If
End Function

Function GetStr(strSrc As String, ByRef wcsDst As PWSTR) As SIZE_T
	If ActiveBasic.IsNothing(strSrc) Then
		wcsDst = 0
		Return 0
	Else
		Return Detail.GetWCStr(strSrc.StrPtr, strSrc.Length As SIZE_T, wcsDst)
	End If
End Function

Function GetStr(strSrc As String, ByRef mbsDst As PSTR) As SIZE_T
	If ActiveBasic.IsNothing(strSrc) Then
		mbsDst = 0
		Return 0
	Else
		Return Detail.GetMBStr(strSrc.StrPtr, strSrc.Length As SIZE_T, mbsDst)
	End If
End Function

Sub GetStrNT(mbszSrc As PSTR, ByRef mbszDst As PSTR)
	mbszDst = mbszSrc
End Sub

Sub GetStrNT(mbsSrc As PSTR, len As SIZE_T, ByRef mbszDst As PSTR)
	mbszDst = mbsSrc
End Sub

Sub GetStrNT(mbszSrc As PSTR, ByRef wcszDst As PWSTR)
	GetStr(mbszSrc, wcszDst)
End Sub

Sub GetStrNT(mbsSrc As PSTR, len As SIZE_T, ByRef wcszDst As PWSTR)
	GetStr(mbsSrc, len, wcszDst)
End Sub

Sub GetStrNT(wcszSrc As PWSTR, ByRef mbszDst As PSTR)
	GetStr(wcszSrc, mbszDst)
End Sub

Sub GetStrNT(wcsSrc As PWSTR, len As SIZE_T, ByRef mbszDst As PSTR)
	GetStr(wcsSrc, len, mbszDst)
End Sub

Sub GetStrNT(wcszSrc As PWSTR, ByRef wcszDst As PWSTR)
	wcszDst = wcszSrc
End Sub

Sub GetStrNT(wcsSrc As PWSTR, len As SIZE_T, ByRef wcszDst As PWSTR)
	wcszDst = wcsSrc
End Sub

Sub GetStrNT(strSrc As String, ByRef mbszDst As PSTR)
	GetStr(strSrc, mbszDst)
End Sub

Sub GetStrNT(strSrc As String, ByRef wcszDst As PWSTR)
	GetStr(strSrc, wcszDst)
End Sub

Function GetWCStr(mbszSrc As PSTR, ByRef wcsDst As PWSTR) As SIZE_T
	Return GetStr(mbszSrc, wcsDst)
End Function

Function GetWCStr(mbsSrc As PSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
	Return GetStr(mbsSrc, len, wcsDst)
End Function

Function GetWCStr(wcszSrc As PWSTR, ByRef wcsDst As PWSTR) As SIZE_T
	Return GetStr(wcszSrc, wcsDst)
End Function

Function GetWCStr(wcsSrc As PWSTR, len As SIZE_T, ByRef wcsDst As PWSTR) As SIZE_T
	Return GetStr(wcsSrc, len, wcsDst)
End Function

Function GetWCStr(strSrc As String, ByRef wcsDst As PWSTR) As SIZE_T
	Return GetStr(strSrc.StrPtr, strSrc.Length As SIZE_T, wcsDst)
End Function

Function GetMBStr(mbszSrc As PWSTR, ByRef mbsDst As PSTR) As SIZE_T
	Return GetStr(mbszSrc, mbsDst)
End Function

Function GetMBStr(wcsSrc As PWSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
	Return GetStr(wcsSrc, len, mbsDst)
End Function

Function GetMBStr(mbszSrc As PSTR, ByRef mbsDst As PSTR) As SIZE_T
	Return GetStr(mbszSrc, mbsDst)
End Function

Function GetMBStr(mbsSrc As PSTR, len As SIZE_T, ByRef mbsDst As PSTR) As SIZE_T
	Return GetStr(mbsSrc, len, mbsDst)
End Function

Function GetMBStr(strSrc As String, ByRef mbsDst As PSTR) As SIZE_T
	Return GetStr(strSrc.StrPtr, strSrc.Length As SIZE_T, mbsDst)
End Function

Function GetTCStr(mbszSrc As PSTR, ByRef tcsDst As PCTSTR) As SIZE_T
	Return GetStr(mbszSrc, tcsDst)
End Function

Function GetTCStr(mbsSrc As PSTR, len As SIZE_T, ByRef tcsDst As PCTSTR) As SIZE_T
	Return GetStr(mbsSrc, len, tcsDst)
End Function

Function GetTCStr(wcszSrc As PWSTR, ByRef tcsDst As PCTSTR) As SIZE_T
	Return GetStr(wcszSrc, tcsDst)
End Function

Function GetTCStr(wcsSrc As PWSTR, len As SIZE_T, ByRef tcsDst As PCTSTR) As SIZE_T
	Return GetStr(wcsSrc, len, tcsDst)
End Function

Function GetTCStr(strSrc As String, ByRef tcsDst As PCTSTR) As SIZE_T
	Return GetStr(strSrc.StrPtr, strSrc.Length As SIZE_T, tcsDst)
End Function

Function ToWCStr(mbsz As PSTR) As PWSTR
	GetStrNT(mbsz, ToWCStr)
End Function

Function ToWCStr(mbs As PSTR, len As SIZE_T) As PWSTR
	GetStrNT(mbs, len, ToWCStr)
End Function

Function ToWCStr(wcsz As PWSTR) As PWSTR
	ToWCStr = wcsz
End Function

Function ToWCStr(wcs As PWSTR, len As SIZE_T) As PWSTR
	ToWCStr = wcs
End Function

Function ToWCStr(s As String) As PWSTR
	GetStrNT(s, ToWCStr)
End Function

Function ToMBStr(mbsz As PSTR) As PSTR
	ToMBStr = mbsz
End Function

Function ToMBStr(mbs As PSTR, len As SIZE_T) As PSTR
	ToMBStr = mbs
End Function

Function ToMBStr(wcsz As PWSTR) As PSTR
	GetStrNT(wcsz, ToMBStr)
End Function

Function ToMBStr(wcs As PWSTR, len As SIZE_T) As PSTR
	GetStrNT(wcs, len, ToMBStr)
End Function

Function ToMBStr(s As String) As PSTR
	GetStrNT(s, ToMBStr)
End Function

Function ToTCStr(mbsz As PSTR) As PCTSTR
	GetStrNT(mbsz, ToTCStr)
End Function

Function ToTCStr(mbs As PSTR, len As SIZE_T) As PCTSTR
	GetStrNT(mbs, len, ToTCStr)
End Function

Function ToTCStr(wcsz As PWSTR) As PCTSTR
	GetStrNT(wcsz, ToTCStr)
End Function

Function ToTCStr(wcs As PWSTR, len As SIZE_T) As PCTSTR
	GetStrNT(wcs, len, ToTCStr)
End Function

Function ToTCStr(s As String) As PCTSTR
	GetStrNT(s, ToTCStr)
End Function

#ifndef UNICODE
TypeDef BoxedStrChar = System.SByte
#else
TypeDef BoxedStrChar = System.UInt16
#endif
