Index: Include/basic/function.sbp
===================================================================
--- Include/basic/function.sbp	(revision 269)
+++ Include/basic/function.sbp	(revision 272)
@@ -13,5 +13,8 @@
 
 #require <Classes/System/Math.ab>
+#require <Classes/System/DateTime.ab>
+#require <Classes/System/Text/StringBuilder.ab>
 #require <Classes/ActiveBasic/Math/Math.ab>
+#require <Classes/ActiveBasic/Strings/Strings.ab>
 
 
@@ -92,10 +95,10 @@
 End Function
 
-Const RAND_MAX=&H7FFFFFFF
-Dim _System_RndNext=1 As DWord
+Const RAND_MAX = &H7FFFFFFF
+Dim _System_RndNext = 1 As DWord
 
 Function rand() As Long
 	_System_RndNext = _System_RndNext * 1103515245 + 12345
-	rand = _System_RndNext >> 1
+	rand = (_System_RndNext >> 1) As Long
 End Function
 
@@ -233,6 +236,5 @@
 
 Function Chr$(code As StrChar) As String
-	Chr$ = ZeroString(1)
-	Chr$[0] = code
+	Chr$ = New String(code, 1)
 End Function
 
@@ -252,12 +254,10 @@
 Function ChrW(c As UCSCHAR) As String
 	If c <= &hFFFF Then
-		ChrW.ReSize(1)
-		ChrW[0] = c As WCHAR
+		Return New String(c As StrChar, 1)
 	ElseIf c < &h10FFFF Then
-		ChrW.ReSize(2)
-		ChrW[0] = &hD800 Or (c >> 10)
-		ChrW[1] = &hDC00 Or (c And &h3FF)
-	Else
-		' OutOfRangeException
+		Dim t[1] = [&hD800 Or (c >> 10), &hDC00 Or (c And &h3FF)] As StrChar
+		Return New String(t, 2)
+	Else
+		'ArgumentOutOfRangeException
 	End If
 End Function
@@ -265,25 +265,27 @@
 
 Function Date$() As String
-	Dim st As SYSTEMTIME
-	GetLocalTime(st)
+	Dim date = DateTime.Now
+	Dim buf = New System.Text.StringBuilder(10)
 
 	'year
-	Date$=Str$(st.wYear)
+	buf.Append(date.Year)
 
 	'month
-	If st.wMonth<10 Then
-		Date$=Date$+"/0"
-	Else
-		Date$=Date$+"/"
-	End If
-	Date$=Date$+Str$(st.wMonth)
+	If date.Month < 10 Then
+		buf.Append("/0")
+	Else
+		buf.Append("/")
+	End If
+	buf.Append(date.Month)
 
 	'day
-	If st.wDay<10 Then
-		Date$=Date$+"/0"
-	Else
-		Date$=Date$+"/"
-	End If
-	Date$=Date$+Str$(st.wDay)
+	If date.Day < 10 Then
+		buf.Append("/0")
+	Else
+		buf.Append("/")
+	End If
+	buf.Append(date.Day)
+
+	Date$ = buf.ToString
 End Function
 
@@ -358,63 +360,49 @@
 End Function
 
-Function Left$(buf As String, length As Long) As String
-	Left$ = ZeroString(length)
-	memcpy(StrPtr(Left$), StrPtr(buf), SizeOf (StrChar) * length)
-End Function
-
-Function Mid$(buf As String, StartPos As Long)(ReadLength As Long) As String
-	Dim length As Long
-
-	StartPos--
-	If StartPos<0 Then
-		'error
-		'Debug
-		Exit Function
-	End If
-
-	length=Len(buf)
-	If length<=StartPos Then Exit Function
-
-	If ReadLength=0 Then
-		ReadLength=length-StartPos
-	End If
-
-	If ReadLength>length-StartPos Then
-		ReadLength=length-StartPos
-	End If
-
-	Mid$=ZeroString(ReadLength)
-	memcpy(StrPtr(Mid$), VarPtr(buf.Chars[StartPos]), SizeOf (StrChar) * ReadLength)
-End Function
-
-Function Oct$(num As DWord) As String
-	Dim i As DWord, i2 As DWord
-
-	For i=10 To 1 Step -1
-		If (num \ CDWord(8^i)) And &H07 Then
-			Exit For
-		End If
-	Next
-
-	Oct$=ZeroString(i+1)
-	i2=0
+Function Left$(s As String, length As Long) As String
+	Left$ = s.Substring(0, System.Math.Min(s.Length, length))
+End Function
+
+Function Mid$(s As String, startPos As Long) As String
+	startPos--
+	Mid$ = s.Substring(startPos)
+End Function
+
+Function Mid$(s As String, startPos As Long, readLength = 0 As Long) As String
+	startPos--
+	Dim length = s.Length
+	Mid$ = s.Substring(System.Math.Min(startPos, length), System.Math.Min(readLength, length - startPos))
+End Function
+
+Const _System_MaxFigure_Oct_QW = 22 As DWord 'QWORD_MAX = &o1,777,777,777,777,777,777,777
+Function Oct$(n As QWord) As String
+	Dim s[ELM(_System_MaxFigure_Oct_QW)] As StrChar
+	Dim i = ELM(_System_MaxFigure_Oct_QW) As Long
 	Do
-		Oct$[i2] = &h30 + ((num \ CDWord(8 ^ i)) And &H07) As StrChar ' &h30 = Asc("0")
-		If i=0 Then Exit Do
+		s[i] = ((n And &o7) + &h30) As StrChar '&h30 = Asc("0")
+		n >>= 3
+		If n = 0 Then
+			Return New String(s + i, _System_MaxFigure_Oct_QW - i)
+		End If
 		i--
-		i2++
 	Loop
 End Function
 
-Function Right$(buf As String, length As Long) As String
-	Dim i As Long
-
-	i=Len(buf)
-	If i>length Then
-		Right$=ZeroString(length)
-		memcpy(StrPtr(Right$), VarPtr(buf.Chars[i-length]), SizeOf (StrChar) * length)
-	Else
-		Right$=buf
-	End If
+Const _System_MaxFigure_Oct_DW = 11 As DWord 'DWORD_MAX = &o37,777,777,777
+Function Oct$(n As DWord) As String
+	Dim s[ELM(_System_MaxFigure_Oct_DW)] As StrChar
+	Dim i = ELM(_System_MaxFigure_Oct_DW) As Long
+	Do
+		s[i] = ((n And &o7) + &h30) As StrChar '&h30 = Asc("0")
+		n >>= 3
+		If n = 0 Then
+			Return New String(s + i, _System_MaxFigure_Oct_DW - i)
+		End If
+		i--
+	Loop
+End Function
+
+Function Right$(s As String, length As Long) As String
+	Right$ = s.Substring(System.Math.Max(0, s.Length - length), s.Length)
 End Function
 
@@ -447,5 +435,5 @@
 	'値が0の場合
 	If value = 0 Then
-		_System_FillChar(_System_ecvt_buffer, count As SIZE_T, &H30 As StrChar)
+		ActiveBasic.Strings.ChrFill(_System_ecvt_buffer, count As SIZE_T, &H30 As StrChar)
 		_System_ecvt_buffer[count] = 0
 		dec = 0
@@ -523,9 +511,9 @@
 		buffer[i] = Asc(".")
 		i++
-		memcpy(VarPtr(buffer[i]), VarPtr(temp[1]), SizeOf (StrChar) * 14)
+		ActiveBasic.Strings.ChrCopy(VarPtr(buffer[i]), VarPtr(temp[1]), 14)
 		i += 14
 		buffer[i] = Asc("e")
 		i++
-		_stprintf(VarPtr(buffer[i]), "+%03d", dec - 1)
+		_stprintf(VarPtr(buffer[i]), "+%03d", dec - 1) 'ToDo: __STRING_UNICODE_WINDOWS_ANSI状態への対応
 
 		Return MakeStr(buffer)
@@ -538,9 +526,9 @@
 		buffer[i] = Asc(".")
 		i++
-		memcpy(VarPtr(buffer[i]), VarPtr(temp[1]), SizeOf (StrChar) * 14)
+		ActiveBasic.Strings.ChrCopy(VarPtr(buffer[i]), VarPtr(temp[1]), 14)
 		i+=14
 		buffer[i] = Asc("e")
 		i++
-		_stprintf(VarPtr(buffer[i]), "+%03d", dec - 1)
+		_stprintf(VarPtr(buffer[i]), "+%03d", dec - 1) 'ToDo: __STRING_UNICODE_WINDOWS_ANSI状態への対応
 
 		Return MakeStr(buffer)
@@ -603,5 +591,5 @@
 
 	Dim buf[20] As StrChar
-	buf[20] = 0
+	'buf[20] = 0
 	Dim i = 19 As Long
 	Do
@@ -640,12 +628,11 @@
 	Dim i = 9 As Long
 	Do
-		buf[i] = (x Mod 10 + &h30) As StrChar
+		buf[i] = (x As Int64 Mod 10 + &h30) As StrChar 'Int64への型変換は#117対策
 		x \= 10
 		If x = 0 Then
-			Exit Do
+			Return New String(VarPtr(buf[i]), 10 - i)
 		End If
 		i--
-	Loop
-	Return New String(VarPtr(buf[i]), 10 - i)
+	Loop	
 #endif
 End Function
@@ -679,46 +666,57 @@
 End Function
 
-Function String$(num As Long, buf As String) As String
-	Dim dwStrPtr As DWord
-	Dim length As Long
-
-	length=Len(buf)
-
-	'バッファ領域を確保
-	String$=ZeroString(length*num)
-
-	'文字列をコピー
+Function String$(n As Long, s As StrChar) As String
+	Return New String(s, n)
+End Function
+		
+#ifdef _AB4_COMPATIBILITY_STRING$_
+Function String$(n As Long, s As String) As String
+	If n < 0 Then
+		'Throw ArgumentOutOfRangeException
+	End If
+
+	Dim buf = New System.Text.StringBuilder(s.Length * n)
 	Dim i As Long
-	For i=0 To num-1
-		memcpy(VarPtr(String$.Chars[i*length]), StrPtr(buf), SizeOf (StrChar) * length)
+	For i = 0 To n
+		buf.Append(s)
 	Next
 End Function
+#else
+Function String$(n As Long, s As String) As String
+	If String.IsNullOrEmpty(s) Then
+		Return New String(0 As StrChar, n)
+			Else
+		Return New String(s[0], n)
+	End If
+End Function
+#endif
 
 Function Time$() As String
-	Dim st As SYSTEMTIME
-
-	GetLocalTime(st)
+	Dim time = DateTime.Now
+
+	Dim buf = New System.Text.StringBuilder(8)
 
 	'hour
-	If st.wHour<10 Then
-		Time$="0"
-	End If
-	Time$=Time$+Str$(st.wHour)
+	If time.Hour < 10 Then
+		buf.Append("0")
+	End If
+	buf.Append(time.Hour)
 
 	'minute
-	If st.wMinute<10 Then
-		Time$=Time$+":0"
-	Else
-		Time$=Time$+":"
-	End If
-	Time$=Time$+Str$(st.wMinute)
+	If time.Minute < 10 Then
+		buf.Append(":0")
+	Else
+		buf.Append(":")
+	End If
+	buf.Append(time.Minute)
 
 	'second
-	If st.wSecond<10 Then
-		Time$=Time$+":0"
-	Else
-		Time$=Time$+":"
-	End If
-	Time$=Time$+Str$(st.wSecond)
+	If time.Second < 10 Then
+		buf.Append(":0")
+	Else
+		buf.Append(":")
+	End If
+	buf.Append(time.Second)
+	Time$ = buf.ToString
 End Function
 
@@ -738,5 +736,5 @@
 	If buf[0]=Asc("&") Then
 		temporary = New String( buf )
-		temporary.ToUpper()
+		temporary = temporary.ToUpper()
 		TempPtr = StrPtr(temporary)
 		If TempPtr(1)=Asc("O") Then
@@ -816,13 +814,11 @@
 
 Function Loc(FileNum As Long) As Long
-	Dim NowPos As Long, BeginPos As Long
-
 	FileNum--
 
-	NowPos=SetFilePointer(_System_hFile(FileNum),0,NULL,FILE_CURRENT)
-	BeginPos=SetFilePointer(_System_hFile(FileNum),0,NULL,FILE_BEGIN)
-	SetFilePointer(_System_hFile(FileNum),NowPos-BeginPos,NULL,FILE_BEGIN)
-
-	Loc=NowPos-BeginPos
+	Dim NowPos = SetFilePointer(_System_hFile(FileNum), 0, 0, FILE_CURRENT)
+	Dim BeginPos = SetFilePointer(_System_hFile(FileNum), 0, 0, FILE_BEGIN)
+	SetFilePointer(_System_hFile(FileNum), NowPos - BeginPos, 0, FILE_BEGIN)
+
+	Loc = NowPos - BeginPos
 End Function
 
@@ -851,5 +847,4 @@
 	_System_pGC->__free(lpMem)
 End Sub
-
 
 Function _System_malloc(stSize As SIZE_T) As VoidPtr
@@ -1044,18 +1039,4 @@
 End Function
 
-Sub _System_FillChar(p As PWSTR, n As SIZE_T, c As WCHAR)
-	Dim i As SIZE_T
-	For i = 0 To ELM(n)
-		p[i] = c
-	Next
-End Sub
-
-Sub _System_FillChar(p As PSTR, n As SIZE_T, c As SByte)
-	Dim i As SIZE_T
-	For i = 0 To ELM(n)
-		p[i] = c
-	Next
-End Sub
-
 Function _System_ASCII_IsUpper(c As WCHAR) As Boolean
 	Return c As DWord - &h41 < 26 ' &h41 = Asc("A")
@@ -1096,96 +1077,4 @@
 Function _System_ASCII_ToUpper(c As SByte) As SByte
 	Return _System_ASCII_ToUpper(c As Byte As WCHAR) As Byte As SByte
-End Function
-
-Function _System_ChrCpy(dst As PCWSTR, src As PCWSTR, size As SIZE_T) As PCWSTR
-	memcpy(dst, src, size * SizeOf (WCHAR))
-	Return dst
-End Function
-
-Function _System_ChrCpy(dst As PCSTR, src As PCSTR, size As SIZE_T) As PCSTR
-	memcpy(dst, src, size)
-	Return dst
-End Function
-
-Function _System_StrCmp(s1 As PCSTR, s2 As PCSTR) As Long
-	Dim i = 0 As SIZE_T
-	While s1[i] = s2[i]
-		If s1[i] = 0 Then
-			Exit While
-		End If
-		i++
-	Wend
-	_System_StrCmp = s1[i] - s2[i]
-End Function
-
-Function _System_StrCmp(s1 As PCWSTR, s2 As PCWSTR) As Long
-	Dim i = 0 As SIZE_T
-	While s1[i] = s2[i]
-		If s1[i] = 0 Then
-			Exit While
-		End If
-		i++
-	Wend
-	_System_StrCmp = s1[i] - s2[i]
-End Function
-
-Function _System_StrCmpN(s1 As PCSTR, s2 As PCSTR, size As SIZE_T) As Long
-	Dim i = 0 As SIZE_T
-	For i = 0 To ELM(size)
-		_System_StrCmpN = s1[i] - s2[i]
-		If _System_StrCmpN <> 0 Then
-			Exit Function
-		End If
-	Next
-End Function
-
-Function _System_StrCmpN(s1 As PCWSTR, s2 As PCWSTR, size As SIZE_T) As Long
-	Dim i = 0 As SIZE_T
-	For i = 0 To ELM(size)
-		_System_StrCmpN = s1[i] - s2[i]
-		If _System_StrCmpN <> 0 Then
-			Exit Function
-		End If
-	Next
-End Function
-
-Function _System_MemChr(s As PCSTR, c As CHAR, size As SIZE_T) As PCSTR
-	Dim i As SIZE_T
-	For i = 0 To ELM(size)
-		If s[i] = c Then
-			Return VarPtr(s[i])
-		End If
-	Next
-	Return 0
-End Function
-
-Function _System_MemChr(s As PCWSTR, c As WCHAR, size As SIZE_T) As PCWSTR
-	Dim i As SIZE_T
-	For i = 0 To ELM(size)
-		If s[i] = c Then
-			Return VarPtr(s[i])
-		End If
-	Next
-	Return 0
-End Function
-
-Function _System_MemPBrk(str As PCSTR, cStr As SIZE_T, Chars As PCSTR, cChars As SIZE_T) As PCSTR
-	Dim i As SIZE_T
-	For i = 0 To ELM(cStr)
-		If _System_MemChr(Chars, str[i], cChars) Then
-			Return VarPtr(str[i])
-		End If
-	Next
-	Return 0
-End Function
-
-Function _System_MemPBrk(str As PCWSTR, cStr As SIZE_T, Chars As PCWSTR, cChars As SIZE_T) As PCWSTR
-	Dim i As SIZE_T
-	For i = 0 To ELM(cStr)
-		If _System_MemChr(Chars, str[i], cChars) Then
-			Return VarPtr(str[i])
-		End If
-	Next
-	Return 0
 End Function
 
