File: stringitem.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (209 lines) | stat: -rw-r--r-- 4,837 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
   FALCON - The Falcon Programming Language.
   FILE: string.cpp

   Item-oriented Core String implementation.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Sat, 03 Jan 2009 23:43:11 +0100

   -------------------------------------------------------------------
   (C) Copyright 2009: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

#include <falcon/string.h>
#include <falcon/item.h>
#include <falcon/error.h>
#include <falcon/vm.h>
#include <stdio.h>

namespace Falcon
{

Item::Item( const String &str )
{
   CoreString* cstr = new CoreString( str );
   cstr->bufferize();
   setString( cstr );
}


StringGarbage::~StringGarbage()
{
}

bool StringGarbage::finalize()
{
   delete m_str;
   // as we're in m_str, we are destroyed here.

   return true; // prevent destructor to be called.
}

void String::readProperty( const String &prop, Item &item )
{
   VMachine *vm = VMachine::getCurrent();
   fassert( vm != 0 );

   // try to find a generic method
   CoreClass* cc = vm->getMetaClass( FLC_ITEM_STRING );
   if ( cc != 0 )
   {
      uint32 id;
      if( cc->properties().findKey( prop, id ) )
      {
         item = *cc->properties().getValue( id );
         item.methodize( this );
         return;
      }
   }

   String extra;
   item.typeName( extra );
   extra.A( '.' ).A( prop );
   throw new AccessError( ErrorParam( e_prop_acc, __LINE__ ).extra( extra ) );
}


void String::readIndex( const Item &index, Item &target )
{

   switch( index.type() )
   {
      case FLC_ITEM_INT:
         {
            int32 pos = (int32) index.asInteger();
            if ( checkPosBound( pos ) )
            {
               CoreString *gcs = new CoreString();
               gcs->append( getCharAt( pos ) );
               target = gcs;
               return;
            }
         }
         break;

      case FLC_ITEM_NUM:
      {
         int32 pos = (int32) index.asNumeric();
         if ( checkPosBound( pos ) )
         {
            CoreString *gcs = new CoreString();
            gcs->append( getCharAt( pos ) );
            target = gcs;
            return;
         }
      }
      break;

      case FLC_ITEM_RANGE:
      {
         int32 rstart = (int32) index.asRangeStart();
         if ( index.asRangeIsOpen() )
         {
            if ( checkPosBound( rstart ) ) {
               target = new CoreString( *this, rstart );
            }
            else {
               target = new CoreString;
            }
            return;
         }
         else {
            int32 rend = (int32) index.asRangeEnd();
            if ( checkRangeBound( rstart, rend ) )
            {
               target = new CoreString( *this, rstart, rend );
               return;
            }
            else {
               target = new CoreString;
            }
            return;
         }
      }
      break;

      case FLC_ITEM_REFERENCE:
         readIndex( index.asReference()->origin(), target );
         return;

   }

   throw new AccessError( ErrorParam( e_arracc, __LINE__ ).extra( "string" ) );
}


void String::writeIndex( const Item &index, const Item &target )
{

   register int32 pos;

   switch( index.type() )
   {
      case FLC_ITEM_INT:
         pos = (int32) index.asInteger();
         break;

      case FLC_ITEM_NUM:
         pos = (int32) index.asNumeric();
         break;

      case FLC_ITEM_RANGE:
      {
         if ( target.isString() )
         {
            register int pos = (int) index.asRangeStart();
            register int end = (int) (index.asRangeIsOpen() ? this->length() :  index.asRangeEnd());
            if ( checkRangeBound( pos, end ) )
            {
               if ( change( pos, end, *target.asString() ) )
               {
                  return;
               }
            }
         }
      }
      throw new AccessError( ErrorParam( e_arracc, __LINE__ ).extra( "string" ) );

      case FLC_ITEM_REFERENCE:
         writeIndex( index.asReference()->origin(), target );
         return;

      default:
         throw new AccessError( ErrorParam( e_arracc, __LINE__ ).extra( "string" ) );
   }


   if ( target.type() == FLC_ITEM_STRING )
   {
      String *cs_orig = target.asString();
      if( cs_orig->length() > 0 ) {
         if ( checkPosBound( pos ) ) {
            setCharAt( pos, cs_orig->getCharAt(0) );
            return;
         }
      }
   }
   else if( target.isOrdinal() )
   {
      int64 chr = target.forceInteger();
      if ( (chr >= 0) && (chr <= (int64) 0xFFFFFFFF) )
      {
         if ( checkPosBound( pos ) ) {
            setCharAt( pos, (uint32) chr );
            return;
         }
      }
   }

   throw new AccessError( ErrorParam( e_arracc, __LINE__ ).extra( "string" ) );
}



}

/* end of stringitem.cpp */