Untitled

 avatar
unknown
c_cpp
a year ago
1.2 kB
3
Indexable
#include "String.hpp"

#include <cstring>
#include <cctype>

String::String( const char* str )
{
    if( str )
    {
        // Jeżeli podamy ciąg inny niż pusty ("")
        m_length = strlen( str );
        m_value = new char[ m_length + 1 ];
        strcpy( m_value, str );
        m_value[ m_length ] = '\0'; // Czy potrzebne? Przemeśleć
    }
    else
    {
        // Podajemy ciąg pusty ("")
        m_length = 0;
        m_value = new char[ 1 ];
        m_value[ 0 ] = '\0';
    }
}

String::~String()
{
    delete[] m_value;
}

String& String::trim()
{
    ltrim();
    rtrim();

    return *this;
}

void String::ltrim()
{
    int start = 0;

    while( std::isspace( m_value[ start ] ) )
    {
        ++start;
    }

    if( start > 0 )
    {
        for( int i = start; i <= m_length; ++i )
        {
            m_value[ i - start ] = m_value[ i ];
        }
        m_length -= start;
    }
}

void String::rtrim()
{
    int end = m_length - 1;

    while( end >= 0 && std::isspace( m_value[ end ] ) )
    {
        --end;
    }

    m_value[ end + 1 ] = '\0';
    m_length = end + 1;
}
Editor is loading...
Leave a Comment