Index: trunk/Include/Classes/ActiveBasic/Strings/SPrintF.ab
===================================================================
--- trunk/Include/Classes/ActiveBasic/Strings/SPrintF.ab	(revision 456)
+++ trunk/Include/Classes/ActiveBasic/Strings/SPrintF.ab	(revision 457)
@@ -205,5 +205,5 @@
 */
 Function FormatFloatF_Core(s As String, e As Long, negative As Boolean, precision As DWord, ByRef flags As FormatFlags) As System.Text.StringBuilder
-	FormatFloatF_Core = New System.Text.StringBuilder
+	FormatFloatF_Core = New System.Text.StringBuilder(128)
 	With FormatFloatF_Core
 		AppendSign(FormatFloatF_Core, negative, flags)
@@ -818,10 +818,8 @@
 
 	Dim sb = New System.Text.StringBuilder
-	If sb.Length = &hfeeefeee Then Debug
 	With sb
 		Dim prefixFunc = tr.Prefix
 		Dim prefix = prefixFunc(x, flags)
 		sb.Append(prefix)
-		If sb.Length = &hfeeefeee Then Debug
 
 		Dim prefixLen = 0 As DWord
@@ -830,9 +828,9 @@
 		End If
 
-		Dim buf = GC_malloc_atomic((tr.MaxSize + 1) * SizeOf (StrChar)) As *StrChar
+'		Dim buf = GC_malloc_atomic((tr.MaxSize + 1) * SizeOf (StrChar)) As *StrChar
+		Dim buf[MaxSizeLO] As StrChar
 		Dim convertFunc = tr.Convert
 		Dim bufStartPos = convertFunc(buf, x, flags)
 
-		If sb.Length = &hfeeefeee Then Debug
 		Dim len = (tr.MaxSize - bufStartPos) As Long
 		If len < 0 Then
@@ -844,8 +842,6 @@
 
 		.Append(buf, bufStartPos + 1, len)
-		If sb.Length = &hfeeefeee Then Debug
 
 		AdjustFieldWidth(sb, field, flags And (Not (Sign Or Blank)), prefixLen)
-		If sb.Length = &hfeeefeee Then Debug
 	End With
 	FormatIntegerEx = sb.ToString()
@@ -855,4 +851,53 @@
 	End If
 End Function
+Sub FormatIntegerEx(sb As System.Text.StringBuilder, ByRef tr As IntegerConvertTraits, x As QWord, d As DWord, field As DWord, flags As FormatFlags)
+
+	If d = DWORD_MAX Then
+		d = 1
+	Else
+		'精度が指定されているとき、ゼロフラグは無視される。
+		'仕様上、左揃えのときも無視されるが、それはAdjustFieldWidthが行ってくれる。
+		flags And= Not Zero
+	End If
+
+	Dim lastLength = sb.Length
+
+	With sb
+		Dim prefixFunc = tr.Prefix
+		Dim prefix = prefixFunc(x, flags)
+		sb.Append(prefix)
+
+		Dim prefixLen = 0 As DWord
+		If String.IsNullOrEmpty(prefix) = False Then
+			prefixLen = prefix.Length As DWord
+		End If
+
+'		Dim buf = GC_malloc_atomic((tr.MaxSize + 1) * SizeOf (StrChar)) As *StrChar
+		Dim buf[MaxSizeLO] As StrChar
+		Dim convertFunc = tr.Convert
+		Dim bufStartPos = convertFunc(buf, x, flags)
+
+		Dim len = (tr.MaxSize - bufStartPos) As Long
+		If len < 0 Then
+			Debug
+		End If
+		If len < d Then
+			.Append(&h30 As StrChar, d - len)
+		End If
+
+		.Append(buf, bufStartPos + 1, len)
+
+		AdjustFieldWidth(sb, field, flags And (Not (Sign Or Blank)), prefixLen, lastLength)
+	End With
+	Dim t = sb.ToString()
+	
+	If (flags And Cap) = 0 Then
+		Dim len = sb.Length
+		Dim i As Long
+		For i = lastLength To ELM(len)
+			sb[i] = CType.ToLower(sb[i])
+		Next
+	End If
+End Sub
 
 /*!
@@ -864,5 +909,5 @@
 
 /*!
-@brief	文字列をフィールド幅まで満たされるように空白などを挿入する。
+@brief	書式化の仕上げとして、変換部分がフィールド幅まで満たされるように空白などを挿入する。
 @author	Egtra
 @date	2007/10/13
@@ -872,22 +917,23 @@
 @param[in] flags	フラグ
 @param[in] prefixLen	（あれば）接頭辞の文字数。ゼロ埋めする際、この数だけ挿入位置を後ろにする。
+@param[in] offset	変換した部分へのオフセット。AppendではなくInsertを行う際に用いられる。
 sbが"-1"のように負符号を持っている場合は、呼出元でSignフラグ（またはBlank）を立てること。
 */
-Sub AdjustFieldWidth(sb As System.Text.StringBuilder, field As DWord, flags As FormatFlags, prefixLen = 0 As DWord)
+Sub AdjustFieldWidth(sb As System.Text.StringBuilder, field As DWord, flags As FormatFlags, prefixLen = 0 As DWord, offset = 0 As Long)
 	With sb
-		If .Length < field Then
-			Dim embeddedSize = field - .Length
+		Dim len = .Length - offset
+		If len < field Then
+			Dim embeddedSize = field - len
 			If flags And LeftSide Then
 				.Append(&h20, embeddedSize)
 			Else
-				Dim insPos As Long
 				If (flags And Zero) <> 0 Then
+					offset += prefixLen
 					If (flags And Blank) Or (flags And Sign) Then
-						insPos++
+						offset++
 					End If
-					insPos += prefixLen
-					.Insert(insPos, String$(embeddedSize, "0"))
+					.Insert(offset, String$(embeddedSize, "0"))
 				Else
-					.Insert(insPos, String$(embeddedSize, " "))
+					.Insert(offset, String$(embeddedSize, " "))
 				End If
 			End If
@@ -1005,5 +1051,6 @@
 	End If
 
-	s.Append(FormatIntegerEx(ByVal traits, x, precision, field, flags))
+'	s.Append(FormatIntegerEx(ByVal traits, x, precision, field, flags))
+	FormatIntegerEx(s, ByVal traits, x, precision, field, flags)
 End Sub
 
@@ -1028,5 +1075,5 @@
 	Do
 		Dim c = s[i]
-		If Not IsDigit(c) Then Exit Do
+		If Not CType.IsDigit(c) Then Exit Do
 		StrToLong *= 10
 		StrToLong += ((c As DWord) And &h0f) As Long
@@ -1037,17 +1084,4 @@
 	End If
 	p = VarPtr(s[i])
-End Function
-
-/*!
-@brief	文字が十進数字かどうか調べる。
-@author	Egtra
-@date	2007/11/11
-@param[in] c	調べる文字
-@retval True	0から9の文字である
-@retval False	そうでない
-*/
-Function IsDigit(c As StrChar) As Boolean
-	Dim dw = (c As DWord)
-	IsDigit = (dw - &h30) < 10
 End Function
 
Index: trunk/Include/Classes/ActiveBasic/Strings/Strings.ab
===================================================================
--- trunk/Include/Classes/ActiveBasic/Strings/Strings.ab	(revision 456)
+++ trunk/Include/Classes/ActiveBasic/Strings/Strings.ab	(revision 457)
@@ -67,6 +67,4 @@
 	Return s1[i] As Long - s2[i]
 End Function
-
-
 
 Function ChrCmp(s1 As PCWSTR, s2 As PCWSTR, size As SIZE_T) As Long
Index: trunk/Include/Classes/System/IO/TextReader.ab
===================================================================
--- trunk/Include/Classes/System/IO/TextReader.ab	(revision 456)
+++ trunk/Include/Classes/System/IO/TextReader.ab	(revision 457)
@@ -36,4 +36,5 @@
 		ElseIf count < 0 Then
 		End If
+		Read = ReadImpl(buffer, index, count)
 	End Function
 
@@ -80,4 +81,5 @@
 	End Function
 	/*
+	@brief 現在位置からストリームの終わりまで読み込む。
 	@date 2008/02/26
 	@auther Egtra
@@ -93,4 +95,7 @@
 			sb.Append(ch As StrChar)
 		Loop
+	End Function
+
+	Static Function Synchronized(reader As TextReader) As TextReader
 	End Function
 
@@ -118,4 +123,62 @@
 End Class
 
+Namespace Detail
+
+Class SynchronizedTextReader
+	Inherits TextReader
+Public
+	Sub SynchronizedTextReader(reader As TextReader)
+		cs = New ActiveBasic.Windows.CriticalSection
+		base = reader
+	End Sub
+
+	Override Function Peek() As Long
+'		Using lock = cs.Lock
+			Peek = base.Peek
+'		End Using
+	End Function
+
+	Override Function Read() As Long
+'		Using lock = cs.Lock
+			Read = base.Read
+'		End Using
+	End Function
+
+	Override Function ReadLine() As String
+'		Using lock = cs.Lock
+			ReadLine = base.ReadLine
+'		End Using
+	End Function
+
+	Override Function ReadToEnd() As String
+'		Using lock = cs.Lock
+			ReadToEnd = base.ReadToEnd
+'		End Using
+	End Function
+
+Protected
+	Override Sub Dispose(disposing As Boolean)
+		Dim s = Nothing As Stream
+		SetPointer(VarPtr(s) As *VoidPtr, InterlockedExchangePointer(ByVal VarPtr(base) As *VoidPtr, 0))
+		If disposing Then
+			If Not ActiveBasic.IsNothing(s) Then
+				s.Dispose()
+			End If
+			cs.Dispose()
+		End If
+	End Sub
+
+	Override Function ReadImpl(buffer As *StrChar, index As Long, count As Long) As Long
+'		Using lock = cs.Lock
+			ReadImpl = base.ReadImpl(buffer, index, count)
+'		End Using
+	End Function
+Private
+	cs As ActiveBasic.Windows.CriticalSection
+	base As TextReader
+End Class
+
+End Namespace
+
 End NameSpace
 End NameSpace
Index: trunk/Include/Classes/System/Math.ab
===================================================================
--- trunk/Include/Classes/System/Math.ab	(revision 456)
+++ trunk/Include/Classes/System/Math.ab	(revision 457)
@@ -127,8 +127,7 @@
 
 	Static Function Ceiling(x As Double) As Long
-		If Floor(x) = x then
-			Return x As Long
-		Else
-			Return Floor(x) + 1
+		Ceiling = Floor(x)
+		If Ceiling <> x Then
+			Ceiling++
 		End If
 	End Function
@@ -145,6 +144,5 @@
 
 	Static Function Cosh(value As Double) As Double
-		Dim t As Double
-		t = Math.Exp(value)
+		Dim t = Math.Exp(value)
 		return (t + 1 / t) * 0.5
 	End Function
@@ -156,6 +154,6 @@
 
 	Static Function DivRem(x As Int64, y As Int64, ByRef ret As Int64) As Int64
-		ret = x - (x \ y) * y
-		return x \ y
+		DivRem = x \ y
+		ret = x - (DivRem) * y
 	End Function
 
@@ -196,8 +194,4 @@
 		Return Int(value)
 	End Function
-
-	'GetHashCode
-
-	'GetType
 
 	Static Function IEEERemainder(x As Double, y As Double) As Double
@@ -513,6 +507,4 @@
 
 	Static Function Sqrt(x As Double) As Double
-		Dim s As Double, last As Double
-		Dim i As *Word, j As Long, jj As Long, k As Long
 		If x > 0 Then
 			If ActiveBasic.Math.IsInf(x) Then
@@ -520,13 +512,14 @@
 			Else
 				Sqrt = x
-				i = (VarPtr(Sqrt) + 6) As *Word
-				jj = GetWord(i)
-				j = jj >> 5
-				k = jj And &h0000001f
-				j = (j+  511) << 4 + k
+				Dim i = (VarPtr(Sqrt) + 6) As *Word
+				Dim jj = GetWord(i) As Long
+				Dim j = jj >> 5 As Long
+				Dim k = (jj And &h0000001f) As Long
+				j = (j + 511) << 4 + k
 				SetWord(i, j)
+				Dim last As Double
 				Do
 					last = Sqrt
-					Sqrt = (x /Sqrt + Sqrt) * 0.5
+					Sqrt = (x / Sqrt + Sqrt) * 0.5
 				Loop While Sqrt <> last
 			End If
Index: trunk/Include/Classes/System/String.ab
===================================================================
--- trunk/Include/Classes/System/String.ab	(revision 456)
+++ trunk/Include/Classes/System/String.ab	(revision 457)
@@ -27,5 +27,5 @@
 
 	Class String
-	'	Inherits IComparable, ICloneable, IConvertible, IEnumerable
+		Implements /*IComparable, ICloneable, IConvertible, IComparable<String>, IEnumerable, IEnumerable<StrChar>, IEquatable<String>*/
 
 		m_Length As Long
@@ -34,9 +34,7 @@
 		Sub validPointerCheck(p As VoidPtr, size = 1 As Long)
 			If p As ULONG_PTR < &h10000 Then
-				'Throw ArgumentException
-				Debug
+				Throw New ArgumentException
 			ElseIf IsBadReadPtr(p, size As ULONG_PTR) Then
-				'Throw ArgumentException
-				Debug
+				Throw New ArgumentException
 			End If
 		End Sub
@@ -153,49 +151,49 @@
 
 		Const Function Operator == (y As String) As Boolean
-			Return String.Compare(This, y) = 0
+			Return Compare(This, y) = 0
 		End Function
 
 		Const Function Operator == (y As *StrChar) As Boolean
-			Return String.Compare(This, y) = 0
+			Return Compare(This, y) = 0
 		End Function
 
 		Const Function Operator <> (y As String) As Boolean
-			Return String.Compare(This, y) <> 0
+			Return Compare(This, y) <> 0
 		End Function
 
 		Const Function Operator <> (y As *StrChar) As Boolean
-			Return String.Compare(This, y) <> 0
+			Return Compare(This, y) <> 0
 		End Function
 
 		Const Function Operator < (y As String) As Boolean
-			Return String.Compare(This, y) < 0
+			Return Compare(This, y) < 0
 		End Function
 
 		Const Function Operator < (y As *StrChar) As Boolean
-			Return String.Compare(This, y) < 0
+			Return Compare(This, y) < 0
 		End Function
 
 		Const Function Operator > (y As String) As Boolean
-			Return String.Compare(This, y) > 0
+			Return Compare(This, y) > 0
 		End Function
 
 		Const Function Operator > (y As *StrChar) As Boolean
-			Return String.Compare(This, y) > 0
+			Return Compare(This, y) > 0
 		End Function
 
 		Const Function Operator <= (y As String) As Boolean
-			Return String.Compare(This, y) <= 0
+			Return Compare(This, y) <= 0
 		End Function
 
 		Const Function Operator <= (y As *StrChar) As Boolean
-			Return String.Compare(This, y) <= 0
+			Return Compare(This, y) <= 0
 		End Function
 
 		Const Function Operator >= (y As String) As Boolean
-			Return String.Compare(This, y) >= 0
+			Return Compare(This, y) >= 0
 		End Function
 
 		Const Function Operator >= (y As *StrChar) As Boolean
-			Return String.Compare(This, y) >= 0
+			Return Compare(This, y) >= 0
 		End Function
 
@@ -205,22 +203,24 @@
 
 	Public
+		'Compareなどで、x.Charsではなく、StrPtr(x)と書く理由は、xがNothingの場合のため。
+
 		Static Function Compare(x As String, indexX As Long, y As String, indexY As Long, length As Long) As Long
-			Return String.CompareOrdinal(x, indexX, y, indexY, length)
+			Return CompareOrdinal(x, indexX, y, indexY, length)
 		End Function
 
 		Static Function CompareOrdinal(x As String, y As String) As Long
-			Return String.CompareOrdinal(x.Chars, y.Chars)
+			Return CompareOrdinal(StrPtr(x), StrPtr(y))
 		End Function
 
 		Static Function CompareOrdinal(x As String, indexX As Long, y As String, indexY As Long, length As Long) As Long
-			Return String.CompareOrdinal(x.Chars, indexX, y.Chars, indexY, length)
+			Return CompareOrdinal(StrPtr(x), indexX, StrPtr(y), indexY, length)
 		End Function
 	Private
 		Static Function Compare(x As String, y As *StrChar) As Long
-			Return String.CompareOrdinal(x, y)
+			Return CompareOrdinal(x, y)
 		End Function
 
 		Static Function CompareOrdinal(x As String, y As *StrChar) As Long
-			Return String.CompareOrdinal(x.Chars, y)
+			Return CompareOrdinal(StrPtr(x), y)
 		End Function
 
@@ -257,5 +257,5 @@
 		Function CompareTo(y As Object) As Long
 			If Not Object.Equals(This.GetType(), y.GetType()) Then
-				Throw New ArgumentException("String.CompareTo: An argument is out of range value.", "y")
+				Throw New ArgumentException("String.CompareTo: The type of the argument y is not String.", "y")
 			End If
 			Return CompareTo(y As String)
@@ -351,6 +351,26 @@
 		End Function
 
+		Static Function Concat(x As String, y As String, z As String) As String
+			Dim sb = New Text.StringBuilder(removeNull(x).Length + removeNull(y).Length + removeNull(z).Length)
+			sb.Append(x).Append(y).Append(z)
+			Concat = sb.ToString
+		End Function
+
+		Static Function Concat(x As String, y As String, z As String, w As String) As String
+			Dim sb = New Text.StringBuilder(removeNull(x).Length + removeNull(y).Length + removeNull(z).Length + removeNull(w).Length)
+			sb.Append(x).Append(y).Append(z).Append(w)
+			Concat = sb.ToString
+		End Function
+
 		Static Function Concat(x As Object, y As Object) As String
-			Return String.Concat(x.ToString, y.ToString)
+			Return Concat(x.ToString, y.ToString)
+		End Function
+
+		Static Function Concat(x As Object, y As Object, z As Object) As String
+			Return Concat(x.ToString, y.ToString, z.ToString)
+		End Function
+
+		Static Function Concat(x As Object, y As Object, z As Object, w As Object) As String
+			Return Concat(x.ToString, y.ToString, z.ToString, w.ToString)
 		End Function
 
@@ -575,5 +595,9 @@
 			Clone = This
 		End Function
-
+/*
+		Function Clone() As Object
+			Clone = This
+		End Function
+*/
 		Static Function Copy(s As String) As String
 			Copy = New String(s.Chars, s.m_Length)
@@ -656,4 +680,12 @@
 			End If
 		End Sub
+
+		Static Function removeNull(s As String) As String
+			If ActiveBasic.IsNothing(s) Then
+				removeNull = Empty
+			Else
+				removeNull = s
+			End If
+		End Function
 	End Class
 
Index: trunk/Include/Classes/System/Text/StringBuilder.ab
===================================================================
--- trunk/Include/Classes/System/Text/StringBuilder.ab	(revision 456)
+++ trunk/Include/Classes/System/Text/StringBuilder.ab	(revision 457)
@@ -182,8 +182,4 @@
 		ElseIf c > Capacity Then
 			Dim p = GC_malloc_atomic((c + 1) * SizeOf (StrChar)) As *StrChar
-			If p = 0 Then
-				'Throw OutOfMemoryException
-				Debug
-			End If
 			ActiveBasic.Strings.ChrCopy(p, chars, size As SIZE_T)
 			chars = p
@@ -488,9 +484,4 @@
 		This.size = 0
 		This.chars = GC_malloc_atomic((This.capacity + 1) * SizeOf (StrChar))
-		If chars = 0 Then
-			'Throw OutOfMemoryException
-			Debug
-		End If
-
 	End Sub
 
