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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! PyString - fixed size, immutable, unicode character buffer type.
//!
//! ```ignore
//! str()
//! "BY THE POWER OF GREYSKULL!"
//! ```
//!
use std::borrow::Borrow;
use std::fmt;
use std::ops::Deref;
use std::hash::{Hash, Hasher};
use std::collections::hash_map::DefaultHasher;
use std::str::FromStr;

use num::ToPrimitive;

use ::modules::precondition::{check_args, check_kwargs};
use ::api::result::Error;
use ::api::method;
use ::api::method::*;
use ::api::RtObject;
use ::api::selfref::{self, SelfRef};
use ::api::typing::{self, BuiltinType};
use ::api::{self, RtValue};
use ::resources::strings;
use ::api::result::{RtResult, ObjectResult};
use ::runtime::Runtime;
use ::runtime::traits::{IntegerProvider, BooleanProvider, StringProvider, DefaultStringProvider,
               FunctionProvider, IteratorProvider};
use ::modules::builtins::Type;
use ::objects::collection::sequence;
use ::system::primitives::{SignatureBuilder};
use ::system::primitives as rs;


const TYPE_NAME: &'static str = "str";


pub struct PyStringType {
    pub empty: RtObject,
}


impl typing::BuiltinType for PyStringType {
    type T = PyString;
    type V = rs::String;

    #[allow(unused_variables)]
    fn new(&self, rt: &Runtime, value: Self::V) -> RtObject {
        PyStringType::inject_selfref(PyStringType::alloc(value))
    }

    fn init_type() -> Self {
        PyStringType { empty: PyStringType::inject_selfref(PyStringType::alloc("".to_string())) }
    }


    fn inject_selfref(value: Self::T) -> RtObject {
        let object = RtObject::new(Type::Str(value));
        let new = object.clone();

        match object.as_ref() {
            &Type::Str(ref string) => {
                string.rc.set(&object.clone());
            }
            _ => unreachable!(),
        }
        new
    }


    fn alloc(value: Self::V) -> Self::T {
        PyString {
            value: StringValue(value),
            rc: selfref::RefCount::default(),
        }
    }
}


pub struct StringValue(pub rs::String);
pub type PyString = RtValue<StringValue>;


impl PyString {
    /* Missing
        [('__ceil__', <function int.__ceil__>),
        ('__class__', int),
        ('__dir__', <function int.__dir__>),
        ('__floor__', <function int.__floor__>),
        ('__format__', <function int.__format__>),
        ('__getnewargs__', <function int.__getnewargs__>),
        ('__init_subclass__', <function int.__init_subclass__>),
        ('__new__', <function int.__new__>),
        ('__reduce__', <function int.__reduce__>),
        ('__reduce_ex__', <function int.__reduce_ex__>),
        ('__round__', <function int.__round__>),
        ('__sizeof__', <function int.__sizeof__>),
        ('__subclasshook__', <function int.__subclasshook__>),
        ('__trunc__', <function int.__trunc__>),
        ('bit_length', <function int.bit_length>),
        ('conjugate', <function int.conjugate>),
        ('denominator', 1),
        ('from_bytes', <function int.from_bytes>),
        ('imag', 0),
        ('numerator', 1),
        ('real', 1),
        ('to_bytes', <function int.to_bytes>)]
    */
    pub fn get_attribute(&self, rt: &Runtime, name: &str) -> ObjectResult {
        match name {
            "__doc__"           => self.try_get_name(rt, name),
            "__abs__"           |
            "__bool__"          |
            "__float__"         |
            "__hash__"          |
            "__index__"         |
            "__int__"           |
            "__invert__"        |
            "__neg__"           |
            "__pos__"           |
            "__repr__"          |
            "__str__"           => self.try_get_unary_method(rt, name),
            "__add__"           |
            "__and__"           |
            "__delattr__"       |
            "__divmod__"        |
            "__eq__"            |
            "__floordiv__"      |
            "__ge__"            |
            "__getattribute__"  |
            "__gt__"            |
            "__le__"            |
            "__lshift__"        |
            "__lt__"            |
            "__mod__"           |
            "__mul__"           |
            "__ne__"            |
            "__or__"            |
            "__radd__"          |
            "__rand__"          |
            "__rdivmod__"       |
            "__rfloordiv__"     |
            "__rlshift__"       |
            "__rmod__"          |
            "__rmul__"          |
            "__ror__"           |
            "__rrshift__"       |
            "__rshift__"        |
            "__rsub__"          |
            "__rtruediv__"      |
            "__rxor__"          |
            "__sub__"           |
            "__truediv__"       |
            "__xor__"           => self.try_get_binary_method(rt, name),
            "__pow__"           |
            "__rpow__"          |
            "__setattr__"       => self.try_get_ternary_method(rt, name),
            missing => return Err(Error::attribute(
                &strings_error_no_attribute!(TYPE_NAME, missing)))
        }
    }

    fn try_get_name(&self, rt: &Runtime, name: &str) -> ObjectResult {
        match name {
            "__doc__" => Ok(rt.str(strings::INT_DOC_STRING)),
            missing => Err(Error::attribute(
                &strings_error_no_attribute!(TYPE_NAME, missing)))
        }
    }

    fn try_get_unary_method(&self, rt: &Runtime, name: &str) -> ObjectResult {
        let func = match name {
            "__abs__"       => {PyString::op_abs},
            "__bool__"      => {PyString::op_bool},
            "__float__"     => {PyString::op_float},
            "__hash__"      => {PyString::op_hash},
            "__index__"     => {PyString::op_index},
            "__int__"       => {PyString::op_int},
            "__invert__"    => {PyString::op_invert},
            "__neg__"       => {PyString::op_neg},
            "__pos__"       => {PyString::op_pos},
            "__repr__"      => {PyString::op_repr},
            "__str__"       => {PyString::op_str},
            missing => return Err(Error::attribute(
                &strings_error_no_attribute!(TYPE_NAME, missing)))
        };

        unary_method_wrapper!(self, TYPE_NAME, name, rt, Type::Str, func)
    }

    fn try_get_binary_method(&self, rt: &Runtime, name: &str) -> ObjectResult {
        let func = match name {
            "__add__"          => {PyString::op_add},
            "__and__"          => {PyString::op_and},
            "__delattr__"      => {PyString::op_delattr},
            "__divmod__"       => {PyString::op_divmod},
            "__eq__"           => {PyString::op_eq},
            "__floordiv__"     => {PyString::op_floordiv},
            "__ge__"           => {PyString::op_ge},
            "__getattribute__" => {PyString::op_getattribute},
            "__gt__"           => {PyString::op_gt},
            "__le__"           => {PyString::op_le},
            "__lshift__"       => {PyString::op_lshift},
            "__lt__"           => {PyString::op_lt},
            "__mod__"          => {PyString::op_mod},
            "__mul__"          => {PyString::op_mul},
            "__ne__"           => {PyString::op_ne},
            "__or__"           => {PyString::op_or},
            "__radd__"         => {PyString::op_radd},
            "__rand__"         => {PyString::op_rand},
            "__rdivmod__"      => {PyString::op_rdivmod},
            "__rfloordiv__"    => {PyString::op_rfloordiv},
            "__rlshift__"      => {PyString::op_rlshift},
            "__rmod__"         => {PyString::op_rmod},
            "__rmul__"         => {PyString::op_rmul},
            "__ror__"          => {PyString::op_ror},
            "__rrshift__"      => {PyString::op_rrshift},
            "__rshift__"       => {PyString::op_rshift},
            "__rsub__"         => {PyString::op_rsub},
            "__rtruediv__"     => {PyString::op_rtruediv},
            "__rxor__"         => {PyString::op_rxor},
            "__sub__"          => {PyString::op_sub},
            "__truediv__"      => {PyString::op_truediv},
            "__xor__"          => {PyString::op_xor},
            missing => return Err(Error::attribute(
                &strings_error_no_attribute!(TYPE_NAME, missing)))
        };

        binary_method_wrapper!(self, TYPE_NAME, name, rt, Type::Str, func)
    }

    fn try_get_ternary_method(&self, rt: &Runtime, name: &str) -> ObjectResult {
        let func = match name {
            "__pow__"          => {PyString::op_pow},
            //"__rpow__"         => {PyString::op_rpow},
            "__setattr__"      => {PyString::op_setattr},
            missing => return Err(Error::attribute(
                &strings_error_no_attribute!(TYPE_NAME, missing)))
        };

        ternary_method_wrapper!(self, TYPE_NAME, name, rt, Type::Str, func)
    }

}

impl fmt::Debug for PyString {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "String {{ {:?} }}", self.value.0)
    }
}


impl api::PyAPI for PyString {}

/// `self.rhs`
impl method::GetAttr for PyString {
    fn op_getattr(&self, rt: &Runtime, name: &RtObject) -> ObjectResult {

        match name.as_ref() {
            &Type::Str(ref pystring) => {
                let string = pystring.value.0.clone();
                self.get_attribute(&rt, &string)
            }
            other => Err(Error::typerr(&format!(
                "getattr <{}>' requires string for attribute names, not {}",
                TYPE_NAME, other.debug_name())))
        }
    }
}

impl method::Hashed for PyString {
    fn op_hash(&self, rt: &Runtime) -> ObjectResult {
        match self.native_hash() {
            Ok(value) => Ok(rt.int(rs::Integer::from(value))),
            Err(err) => Err(err),
        }
    }

    fn native_hash(&self) -> RtResult<rs::HashId> {
        let mut s = DefaultHasher::new();
        self.value.0.hash(&mut s);
        Ok(s.finish())
    }
}


impl method::StringCast for PyString {

    #[allow(unused_variables)]
    fn op_str(&self, rt: &Runtime) -> ObjectResult {
        self.rc.upgrade()
    }

    fn native_str(&self) -> RtResult<rs::String> {
        Ok(self.value.0.clone())
    }

}


impl method::Equal for PyString {
    fn op_eq(&self, rt: &Runtime, rhs: &RtObject) -> ObjectResult {
        match self.native_eq(rhs.as_ref()) {
            Ok(value) => Ok(rt.bool(value)),
            _ => unreachable!(),
        }
    }

    fn native_eq(&self, rhs: &Type) -> RtResult<rs::Boolean> {
        match rhs {
            &Type::Str(ref string) => Ok(self.value.0.eq(&string.value.0)),
            _ => Ok(false),
        }
    }
}


impl method::BooleanCast for PyString {
    fn op_bool(&self, rt: &Runtime) -> ObjectResult {
        match self.native_bool() {
            Ok(bool) => Ok(rt.bool(bool)),
            Err(err) => Err(err)
        }
    }

    fn native_bool(&self) -> RtResult<rs::Boolean> {
        Ok(!self.value.0.is_empty())
    }
}


impl method::IntegerCast for PyString {
    fn op_int(&self, rt: &Runtime) -> ObjectResult {
        match self.native_int() {
            Ok(int) => Ok(rt.int(int)),
            Err(err) => Err(err)
        }
    }

    fn native_int(&self) -> RtResult<rs::Integer> {
        match rs::Integer::from_str(&self.value.0) {
            Ok(int) => Ok(int),
            Err(_) => Err(Error::value(
                &format!("Invalid literal '{}' for int", self.value.0)))
        }
    }

}


impl method::Add for PyString {

    fn op_add(&self, rt: &Runtime, rhs: &RtObject) -> ObjectResult {
        match rhs.as_ref() {
            &Type::Str(ref other) => Ok(rt.str([&self.value.0[..], &other.value.0[..]].concat())),
            other => Err(Error::typerr(
                &strings_error_bad_operand!("+", "str", other.debug_name()))),
        }
    }

}

impl method::Multiply for PyString {
    fn op_mul(&self, rt: &Runtime, rhs: &RtObject) -> ObjectResult {

        match rhs.as_ref() {
            &Type::Int(ref int) => {
                match int.value.0.to_usize() {
                    Some(int) if int <= 0   => Ok(rt.default_str()),
                    Some(int) if int == 1   => self.rc.upgrade(),
                    Some(int)               => {
                        let value: String = (0..int)
                            .map(|_| self.value.0.clone())
                            .collect::<Vec<_>>()
                            .concat();
                        Ok(rt.str(value))
                    },
                    None                    => {
                        Err(Error::overflow(strings::ERROR_NATIVE_INT_OVERFLOW))
                    },
                }
            }
            other => Err(Error::typerr(
                &strings_error_bad_operand!("*", TYPE_NAME, other.debug_name())))
        }
    }

}


impl method::Contains for PyString {
    fn op_contains(&self, rt: &Runtime, item: &RtObject) -> ObjectResult {
        let truth = self.native_contains(item.as_ref())?;
        Ok(rt.bool(truth))
    }

    fn native_contains(&self, item: &Type) -> RtResult<rs::Boolean> {
        match item {
            &Type::Str(ref string) => {
                Ok(self.value.0.contains(&string.value.0))
            },
            other => Err(Error::typerr(&format!(
                "in <{}>' requires string as left operand, not {}",
                TYPE_NAME, other.debug_name())))
        }
    }
}

impl method::Iter for PyString {
    fn op_iter(&self, rt: &Runtime) -> ObjectResult {
        let iter = self.native_iter()?;
        Ok(rt.iter(iter))
    }

    fn native_iter(&self) -> RtResult<rs::Iterator> {
        match self.rc.upgrade() {
            Ok(selfref) => Ok(rs::Iterator::new(&selfref)?),
            Err(err) => Err(err)
        }
    }
}

impl method::Length for PyString {
    fn op_len(&self, rt: &Runtime) -> ObjectResult {
        Ok(rt.int(self.value.0.len()))
    }

    fn native_len(&self) -> RtResult<rs::Integer> {
        Ok(rs::Integer::from(self.value.0.len()))
    }
}

impl method::GetItem for PyString {
    #[allow(unused_variables)]
    fn op_getitem(&self, rt: &Runtime, item: &RtObject) -> ObjectResult {
        self.native_getitem(item.as_ref())
    }

    fn native_getitem(&self, index: &Type) -> ObjectResult {
        let substr = match index {
            &Type::Int(ref int) => {
                let byte = sequence::get_index(&self.value.0.as_bytes(), &int.value.0)?;
                // TODO: {T3093} Determine the best policy for strings as a sequence since any kind
                // of encoding ruins the uniform treatment of bytes as a singular index of a
                // string.
                String::from_utf8_lossy(&[byte][..]).to_string()
            }
            _ => return Err(Error::typerr("string indices must be integers")),
        };

        Ok(PyStringType::inject_selfref(PyStringType::alloc(substr)))
    }
}


method_not_implemented!(PyString,
    AbsValue   AddItem   Append   Await   BitwiseAnd   BitwiseOr   
    BytesCast   Call   Clear   Close   ComplexCast   Count   
    DelAttr   Delete   DeleteItem   DescriptorGet   DescriptorSet   DescriptorSetName   
    Discard   DivMod   Enter   Exit   Extend   FloatCast   
    FloorDivision   Get  GetAttribute   GreaterOrEqual   GreaterThan
    Id   Index   Init   InPlaceAdd   InPlaceBitwiseAnd   InPlaceBitwiseOr   
    InPlaceDivMod   InPlaceFloorDivision   InPlaceLeftShift   InPlaceMatrixMultiply
    InPlaceModulus   InPlaceMultiply InPlacePow   InPlaceRightShift   InPlaceSubtract
    InPlaceTrueDivision   InPlaceXOr   InvertValue Is   IsDisjoint   IsNot   Items   Keys
    LeftShift LengthHint   LessOrEqual   LessThan   MatrixMultiply   Modulus   NegateValue
    New   Next   NotEqual   Pop   PopItem   PositiveValue  Pow   ReflectedAdd   ReflectedBitwiseAnd
    ReflectedBitwiseOr   ReflectedDivMod   ReflectedFloorDivision ReflectedLeftShift
    ReflectedMatrixMultiply   ReflectedModulus   ReflectedMultiply   ReflectedPow
    ReflectedRightShift ReflectedSubtract   ReflectedTrueDivision   ReflectedXOr   Remove
    Reversed   RightShift Rounding   Send   SetAttr   SetDefault   SetItem   StringFormat
    StringRepresentation   Subtract   Throw   TrueDivision   Update   Values XOr
);


#[cfg(test)]
mod tests {
    use ::runtime::Runtime;

    fn setup() -> (Runtime, ) {
        (Runtime::new(), )
    }

    #[test]
    fn stub() {
        info!("stub");
    }
}