Skip to content

Commit 335947c

Browse files
[string] make basic Format function
1 parent 2a9f22a commit 335947c

File tree

7 files changed

+44
-3
lines changed

7 files changed

+44
-3
lines changed

appveyor.yml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ platform:
99

1010
configuration:
1111
- Debug
12+
- Release
1213

1314
build:
1415
project: C:/projects/cpp-net-framework/build/cpp-net-framework.sln

src/framework/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ cmake_minimum_required (VERSION 2.6)
33
SET(SOURCE
44
System.cxx
55
System/Object.cxx
6+
System/String.cxx
67
Path.cxx
78
System/IO/FileInfo.cxx
89
System/IO/StreamReader.cxx

src/framework/System/Object.cxx

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace System
66
{
77
/// basic to string operation of all base classes
8-
String Object::ToString()
8+
String Object::ToString() const
99
{
1010
return String("Object");
1111
}

src/framework/System/Object.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace System
2424
}
2525

2626
/// function to print this object
27-
virtual String ToString();
27+
virtual String ToString() const;
2828
};
2929
}
3030

src/framework/System/String.cxx

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
#include "String.h"
3+
4+
String String::Format(const String &fmt,const Object &a)
5+
{
6+
String output;
7+
8+
output=fmt;
9+
10+
output = output.Replace("{0}",a.ToString());
11+
12+
return output;
13+
}
14+
15+
16+
String String::Format(const char* fmt,const char* a)
17+
{
18+
return Format(String(fmt),String(a));
19+
}

src/framework/System/String.h

+12-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ namespace System
286286
}
287287

288288
/// turn the string into a string
289-
virtual String ToString()
289+
virtual String ToString() const
290290
{
291291
return (*this);
292292
}
@@ -296,6 +296,17 @@ namespace System
296296
{
297297
m_str.clear();
298298
}
299+
300+
/// TODO change to variable argument
301+
/// for now stick with naive implementation
302+
/// format a string with parameterizd arguments
303+
String Format(const String& fmt,const Object& a);
304+
String Format(const String& fmt,const Object& a,const Object& b);
305+
306+
/// TODO change to variable argument
307+
/// for now stick with naive implementation
308+
String Format(const char* fmt,const char* a);
309+
String Format(const char* fmt,const char* a,const char* b);
299310
};
300311

301312
/// the plus operator for two strings

src/unittests/String_Test.cxx

+9
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,12 @@ TEST(StringTest, Equals)
194194
EXPECT_TRUE(String("Hello").Equals("Hello", Ordinal));
195195
EXPECT_FALSE(String("Hello").Equals("hello", Ordinal));
196196
}
197+
198+
TEST(StringTest, Format)
199+
{
200+
String s;
201+
202+
s = s.Format("ABC{0}DEF","ABC");
203+
204+
EXPECT_EQ(String("ABCABCDEF"),String(s)) << "Wow!" << std::endl;
205+
}

0 commit comments

Comments
 (0)