1 /*---------------------------------------------------------------------------* 2 Project: Horizon 3 File: test_String.cpp 4 5 Copyright (C)2009-2012 Nintendo Co., Ltd. All rights reserved. 6 7 These coded instructions, statements, and computer programs contain 8 proprietary information of Nintendo of America Inc. and/or Nintendo 9 Company Ltd., and are protected by Federal copyright law. They may 10 not be disclosed to third parties or copied or duplicated in any form, 11 in whole or in part, without the prior written consent of Nintendo. 12 13 $Rev: 46347 $ 14 *---------------------------------------------------------------------------*/ 15 16 #include <nn/test/test_String.h> 17 #include <cstring> 18 #include <cstdio> 19 namespace nn{ namespace test{ String()20 String::String() 21 { 22 Initialize(NULL); 23 } 24 String(const char * pBuffer)25 String::String(const char* pBuffer) 26 { 27 Initialize(pBuffer); 28 } 29 String(const String & other)30 String::String(const String& other) 31 { 32 Initialize(other.GetBuffer()); 33 } 34 String(int num)35 String::String(int num) 36 { 37 char buf[11]; 38 ::std::sprintf(buf, "%d", num); 39 Initialize(buf); 40 } 41 operator =(const char * pBuffer)42 String& String::operator=(const char* pBuffer) 43 { 44 Finalize(); 45 Initialize(pBuffer); 46 return *this; 47 } 48 operator =(const String & other)49 String& String::operator =(const String& other) 50 { 51 return *this = other.GetBuffer(); 52 } 53 operator ==(String & other)54 bool String::operator==(String& other) 55 { 56 return other == m_pBuffer; 57 } 58 operator ==(const char * pBuffer)59 bool String::operator==(const char* pBuffer) 60 { 61 return ::std::strcmp(pBuffer, m_pBuffer) == 0; 62 } 63 operator +=(const char * pBuffer)64 String& String::operator+=(const char* pBuffer) 65 { 66 int newLen = ::std::strlen(m_pBuffer) + ::std::strlen(pBuffer) + 1; 67 char* pNewBuffer = new char[newLen]; 68 ::std::strcpy(pNewBuffer, m_pBuffer); 69 ::std::strcat(pNewBuffer, pBuffer); 70 delete [] m_pBuffer; 71 m_pBuffer = pNewBuffer; 72 return *this; 73 } 74 operator +=(const String & other)75 String& String::operator+=(const String& other) 76 { 77 return (*this += other.GetBuffer()); 78 } 79 Finalize()80 void String::Finalize() 81 { 82 delete [] m_pBuffer; 83 } 84 Initialize(const char * pBuffer)85 void String::Initialize(const char* pBuffer) 86 { 87 if(pBuffer == NULL) 88 { 89 m_pBuffer = new char[1]; 90 m_pBuffer[0] = '\0'; 91 } 92 else 93 { 94 m_pBuffer = new char[::std::strlen(pBuffer) + 1]; 95 ::std::strcpy(m_pBuffer, pBuffer); 96 } 97 } 98 ~String()99 String::~String() 100 { 101 delete[] m_pBuffer; 102 } 103 }} 104 105