buildDefString function

void buildDefString(
  1. dynamic value,
  2. BuildingContext context,
  3. int level
)

Implementation

void buildDefString(
    dynamic value,
    BuildingContext context,
    int level
    )
{
    var indent = ' ' * ( level * context.levelSpaceCount );

    if ( value == '' )
    {
        context.lineArray.add( indent + '¨' );
    }
    else if ( value == '['
              || value == '{'
              || value == '('
              || value == 'undefined'
              || value == 'false'
              || value == 'true'
              || value == 'null'
              || value == 'NaN'
              || value == '-Infinity'
              || value == 'Infinity'
              || _hexadecimalIntegerExpression.hasMatch( value )
              || _decimalRealExpression.hasMatch( value )
              || _exponentialDecimalRealExpression.hasMatch( value ) )
    {
        context.lineArray.add( indent + value + '¨' );
    }
    else
    {
        var lineArray = value.split( '\n' );
        var lineCount = lineArray.length;
        var quote;

        if ( lineCount == 1 )
        {
            quote = '';
        }
        else
        {
            quote = context.quote;
        }

        for ( int lineIndex = 0; lineIndex < lineCount; ++lineIndex )
        {
            var line =
                lineArray[ lineIndex ]
                    .replaceAll( '\\', '\\\\' )
                    .replaceAll( '\b', '\\b' )
                    .replaceAll( '\f', '\\f' )
                    .replaceAll( '\r', '\\r' )
                    .replaceAll( '\t', '\\t' )
                    .replaceAll( '\v', '\\v' );

            if ( lineCount == 1 )
            {
                if ( line.startsWith( '\'' )
                     || line.startsWith( '"' )
                     || line.startsWith( '`' )
                     || line.startsWith( '´' ) )
                {
                    line = '\\' + line;
                }

                if ( line.endsWith( ' ' ) )
                {
                    line += '¨';
                }
                else if ( line.endsWith( '¨' ) )
                {
                    line = line.substring( 0, line.length - 1 ) + '\\¨';
                }
            }
            else
            {
                if ( lineIndex == 0 )
                {
                    line = quote + line;
                }

                if ( lineIndex > 0
                     && lineIndex < lineCount - 1 )
                {
                    if ( line.endsWith( quote ) )
                    {
                        line = line.substring( 0, line.length - 1 ) + '\\' + quote;
                    }
                }

                if ( lineIndex == lineCount - 1 )
                {
                    line += quote;
                }
                else if ( line.endsWith( ' ' ) )
                {
                    line += '¨';
                }
                else if ( line.endsWith( '¨' ) )
                {
                    line = line.substring( 0, line.length - 1 ) + '\\¨';
                }
            }

            context.lineArray.add( indent + line );
        }
    }
}