📄 charstring.cpp
字号:
//// This file is part of the "More for C++" library//// Copyright (c) 1999-2003 by Thorsten Goertz (thorsten@morefor.org)//// The "More for C++" library is free software; you can redistribute it and/or// modify it under the terms of the license that comes with this package.//// Read "license.txt" for more details.//// THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED// WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES// OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.////////////////////////////////////////////////////////////////////////////////#include <stdio.h>#include <string.h>#include <more/platform.hpp>#include <more/util/vector.hpp>#include "charstring.hpp"using namespace more;using namespace more::core;using namespace more::util;////////////////////////////////////////////////////////////////////////////////inline const char* constCharPtr( const Array<char>& rConstArrayOfChar ){ return rConstArrayOfChar.getLength( ) > 0 ? &rConstArrayOfChar[0] : "";}////////////////////////////////////////////////////////////////////////////////TString<char>::TString( const char* pcString): m_array( pcString == 0 || *pcString == 0 ? 0 : strlen( pcString ) ){ size_t length = m_array.getLength( ); if( length > 0 ) { memcpy( &m_array[0], pcString, length ); } updateDebugString( );}////////////////////////////////////////////////////////////////////////////////TString<char>::TString( const TString<char>& rString): m_array( rString.m_array ){ updateDebugString( );}////////////////////////////////////////////////////////////////////////////////TString<char>::TString( const Array<char>& rArray): m_array( ){ operator = ( rArray ); updateDebugString( );}////////////////////////////////////////////////////////////////////////////////size_t TString<char>::getLength( ) const{ return m_array.getLength( );}////////////////////////////////////////////////////////////////////////////////const TString<char>& TString<char>::operator =( const char* pcString){ m_array.setLength( 0 ); if( pcString != 0 ) { size_t nLength = strlen( pcString ); if( nLength > 0 ) { m_array.setLength( nLength ); memcpy( &m_array[0], pcString, nLength ); } } updateDebugString( ); return *this;}////////////////////////////////////////////////////////////////////////////////const TString<char>& TString<char>::operator =( const TString<char>& rString){ m_array = rString.m_array; updateDebugString( ); return *this;}////////////////////////////////////////////////////////////////////////////////const TString<char>& TString<char>::operator =( const Array<char>& rArray){ size_t nLength = rArray.getLength( ); while( nLength > 0 && rArray[nLength - 1] == '\0' ) { nLength--; } m_array.setLength( nLength ); if( nLength > 0 ) { memcpy( &m_array[0], &rArray[0], nLength ); } updateDebugString( ); return *this;}////////////////////////////////////////////////////////////////////////////////const TString<char>& TString<char>::operator =( const char cChar){ m_array.setLength( 1 ); m_array[0] = cChar; updateDebugString( ); return *this;}////////////////////////////////////////////////////////////////////////////////TString<char>::operator const char* ( ) const{ return m_array.getLength( ) > 0 ? constCharPtr( m_array ) : "";}////////////////////////////////////////////////////////////////////////////////Array<char> TString<char>::copyToArray( ) const{ size_t nLength = getLength( ); Array<char> result( nLength ); if( nLength > 0 ) { memcpy( &result[0], *this, nLength ); } return result;}////////////////////////////////////////////////////////////////////////////////TString<char> TString<char>::getPrefix( size_t nSize) const{ size_t nLength = getLength( ); size_t nCheckedSize = ( nSize <= nLength ? nSize : nLength ); return TString<char>( m_array, 0, nCheckedSize );}////////////////////////////////////////////////////////////////////////////////TString<char> TString<char>::getSuffix( size_t nSize) const{ size_t nLength = getLength( ); size_t nCheckedSize = ( nSize <= nLength ? nSize : nLength ); return TString<char>( m_array, nLength - nCheckedSize, nCheckedSize );}////////////////////////////////////////////////////////////////////////////////Array<TString<char> > TString<char>::split( const char cSeparator) const{ typedef Vector<TString<char> > StringVector; p<StringVector> pWords = CREATE StringVector; size_t nPos1 = 0; while( nPos1 < m_array.getLength( ) ) { while( nPos1 < m_array.getLength( ) && m_array[nPos1] == cSeparator ) { nPos1++; } if( nPos1 < m_array.getLength( ) ) { size_t nPos2 = nPos1 + 1; while( nPos2 < m_array.getLength( ) && m_array[nPos2] != cSeparator ) { nPos2++; } pWords -> add( TString<char>( m_array, nPos1, nPos2 - nPos1 ) ); nPos1 = nPos2; } } return pWords -> toArray( );}////////////////////////////////////////////////////////////////////////////////TString<char> TString<char>::getSubstring( size_t nPosition, size_t nSize) const{ size_t nLength = getLength( ); size_t nCheckedPos = ( nPosition <= nLength ? nPosition : nLength ); size_t nCheckedSize = ( nCheckedPos + nSize <= nLength ? nSize : nLength - nCheckedPos ); return TString<char>( m_array, nCheckedPos, nCheckedSize );}////////////////////////////////////////////////////////////////////////////////bool TString<char>::find( const TString<char>& rString, size_t nPosition, size_t& rnResult) const{ bool bResult = false; if( nPosition < getLength( ) ) { const char* pcString = *this; char* pcSubstring = strstr( pcString + nPosition, rString ); if( pcSubstring != 0 ) { bResult = true; rnResult = pcSubstring - pcString; } } return bResult;}////////////////////////////////////////////////////////////////////////////////TString<char> TString<char>::toLowerCase( ) const{ Array<char> result( this -> copyToArray( ) ); for( size_t i = 0; i < result.getLength( ); i++ ) { if( result[i] >= 'A' && result[i] <= 'Z' ) { result[i] += ( 'a' - 'A' ); }#ifdef PLATFORM_WIN32 else if( result[i] == '
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -