Changeset 723


Ignore:
Timestamp:
May 5, 2003, 4:04:11 PM (18 years ago)
Author:
orlov
Message:
  • Reworked Char::to_chars() method.
Location:
to-imperative/trunk
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • to-imperative/trunk/library/Convert/to_int.cc

    r719 r723  
    4646          retfail;
    4747        else {
    48           size_t num_len = ShortInt::get_char_len(((ShortInt*)t)->to_int());
     48          size_t num_len = ((ShortInt*)t)->get_char_len();
    4949          if (len > UINTPTR_MAX - num_len)
    5050            RF_LIB_ERROR("Argument too large for conversion");
  • to-imperative/trunk/runtime/rf_char.cc

    r705 r723  
    1 #include "rf_char.ih"
    21#include "rf_expr.ih"
    32#include "rf_types.ih"
    4 #include "rf_short_int.ih"
     3#include "rf_core.hh"
    54
    65namespace rftype
    76{
     7
     8Expr Char::create_expr (const WString& _wstr)
     9{
     10  size_t len = _wstr.get_length();
     11  const wchar_t* pstr = _wstr.get_data();
     12 
     13  Expr  e = Term::create_expr(len);
     14  Term* p = e.get_first();
     15
     16  while (len--)
     17    new(p++) Char(*pstr++);
     18  return e;
     19}
    820
    921Expr Char::create_expr (char const* _str, char const* _locale /* = null */)
     
    3143}
    3244
     45static Expr terms_to_chars (Term const* _p, Term const* _q)
     46{
     47  static const Expr lp = Char::create_expr("(");
     48  static const Expr rp = Char::create_expr(")");
     49
     50  Expr e = empty;
     51
     52  for (; _p < _q; _p++)
     53  {
     54    if (_p->is_ref())
     55      e = e + lp + terms_to_chars(_p->get_first(), _p->get_last()) + rp;
     56    else
     57      e = e + Char::create_expr(WString(*_p));
     58  };
     59
     60  return e;
     61}
     62
    3363Expr Char::to_chars (const Expr& _expr)
    3464{
    35   Term* p = _expr.get_first();
    36   Term* q = _expr.get_last();
    37  
    38   Expr  e = Term::create_expr(Term::get_char_lens(p, q));
    39   Term* r = e.get_first();
    40  
    41   for (; p < q; p++)
    42     r = p->to_chars(r);
    43 
    44   return e;
     65  return terms_to_chars(_expr.get_first(), _expr.get_last());
    4566}
    4667
     
    128149}
    129150
    130 size_t Char::get_char_len (uintptr_t)
    131 {
    132   return 1;
    133 }
    134 
    135 Term* Char::to_chars (uintptr_t _data, Term* _p)
    136 {
    137   new(_p++) Char(_data);
    138   return _p;
    139 }
    140 
    141151short_type_funcs_t Char::funcs = {
    142152  Char::ctor,
     
    145155  Char::compare,
    146156  Char::hash,
    147   Char::to_string,
    148   Char::get_char_len,
    149   Char::to_chars
     157  Char::to_string
    150158};
    151159
  • to-imperative/trunk/runtime/rf_char.hh

    r705 r723  
    33// $Date$
    44// $Author$
     5
     6#ifndef __rf_char_hh__
     7#define __rf_char_hh__
    58
    69#include "rf_types.hh"
     
    2629#endif
    2730
    28   static Expr create_expr (wchar_t const* _wstr) ;
     31  static Expr create_expr (wchar_t const* _wstr, size_t _len = -1) ;
     32 
     33  static Expr create_expr (const WString& _wstr) ;
    2934
    3035  static Expr create_expr (char const* _str, char const* _locale = null) ;
     
    6671  static uint32_t hash (uintptr_t _data) ;
    6772  static pxx::WString to_string (uintptr_t _data) ;
    68   static size_t get_char_len (uintptr_t _data) ;
    69   static Term* to_chars (uintptr_t _data, Term* _p) ;
    7073  static short_type_funcs_t funcs ;
    7174  static TypeRegister reg ;
     
    7578}
    7679
    77 
     80#endif // __rf_char_hh__
  • to-imperative/trunk/runtime/rf_short_int.cc

    r719 r723  
    128128}
    129129
    130 size_t ShortInt::get_char_len(uintptr_t _data) {
    131   intptr_t n = _data;
    132   uintptr_t k = 0;
    133   if (!n) k = 1;
    134   else {
    135     if (n < 0) k++;
    136     while (n) {
    137       n /= 10;
    138       k++;
    139     };
    140   };
    141   return k;
    142 };
    143 
    144 /*Term* ShortInt::to_chars(uintptr_t _data, Term* _p) {
    145   char str[11];
    146   sprintf(str, "%d", _data);
    147   for (char *s = str; *s; s++)
    148     *_p++ = Char(*s);
    149   return _p;
    150 
    151   //  new(_p++) ShortInt(_data);
    152   //  return _p;
    153 };
    154 */
    155 
    156130short_type_funcs_t ShortInt::funcs = {
    157131  ShortInt::ctor,
     
    160134  ShortInt::compare,
    161135  ShortInt::hash,
    162   ShortInt::to_string,
    163   ShortInt::get_char_len,
    164   null // ShortInt::to_chars
     136  ShortInt::to_string
    165137};
    166138
  • to-imperative/trunk/runtime/rf_short_int.hh

    r719 r723  
    3232  inline intptr_t to_int () const ;
    3333
     34  inline size_t get_char_len () const ;
     35
    3436  static uintptr_t ctor (uintptr_t _data) ;
    3537  static void dtor (uintptr_t _data) ;
     
    3840  static uint32_t hash (uintptr_t _data) ;
    3941  static pxx::WString to_string (uintptr_t _data) ;
    40   static size_t get_char_len(uintptr_t _data) ;
    41   static Term* to_chars (uintptr_t _data, Term* _p) ;
    4242  static short_type_funcs_t funcs ;
    4343  static TypeRegister reg ;
  • to-imperative/trunk/runtime/rf_short_int.ih

    r631 r723  
    2323}
    2424
     25inline size_t ShortInt::get_char_len() const
     26{
     27  intptr_t n = int_data2;
     28  uintptr_t k = 0;
     29  if (!n) k = 1;
     30  else {
     31    if (n < 0) k++;
     32    while (n) {
     33      n /= 10;
     34      k++;
     35    };
     36  };
     37  return k;
     38};
     39
    2540}
  • to-imperative/trunk/runtime/rf_term.cc

    r705 r723  
    3939  &Term::to_string_ref,
    4040  &Term::to_string_obj
    41 };
    42 
    43 Term::get_char_len_func_t Term::get_char_len_funcs[term_class_num] = {
    44   &Term::get_char_len_sym,
    45   &Term::get_char_len_ref,
    46   &Term::get_char_len_obj
    47 };
    48 
    49 Term::to_chars_func_t Term::to_chars_funcs[term_class_num] = {
    50   &Term::to_chars_sym,
    51   &Term::to_chars_ref,
    52   &Term::to_chars_obj
    5341};
    5442
     
    176164}
    177165
    178 size_t Term::get_char_len_sym () const
    179 {
    180   return (*(short_type_funcs[get_type()]->get_char_len))(uint_data2);
    181166}
    182167
    183 size_t Term::get_char_len_ref () const
    184 {
    185   size_t len = get_char_lens(get_first(), get_last());
    186   if (len > SIZE_MAX - 2) throw Expr::TooLargeArgument();
    187   return len + 2;
    188 }
    189 
    190 size_t Term::get_char_len_obj () const
    191 {
    192   FATAL("Not implemented yet...");
    193   return 0;
    194 }
    195 
    196 Term* Term::to_chars_sym (Term* _r) const
    197 {
    198   return (*(short_type_funcs[get_type()]->to_chars))(uint_data2, _r);
    199 }
    200 
    201 Term* Term::to_chars_ref (Term* _r) const
    202 {
    203   new(_r++) Char(L'(');
    204 
    205   Term* p = get_first();
    206   Term* q = get_last();
    207 
    208   for (; p < q; p++)
    209     _r = p->to_chars(_r);
    210 
    211   new(_r++) Char(L')');
    212 
    213   return _r;
    214 }
    215 
    216 Term* Term::to_chars_obj (Term*) const
    217 {
    218   FATAL("Not implemented yet...");
    219   return 0;
    220 }
    221 
    222 }
    223 
  • to-imperative/trunk/runtime/rf_term.hh

    r561 r723  
    147147  inline Term& operator = (Term const& _t) ;
    148148
    149   inline operator pxx::WString () ;
    150 
    151149  typedef void (Term::*ctor_func_t)(Term const& _t) ;
    152150  typedef void (Term::*dtor_func_t)() ;
     
    160158  static eq_func_t eq_funcs[term_class_num] ;
    161159  static to_string_func_t to_string_funcs[term_class_num] ;
    162   static get_char_len_func_t get_char_len_funcs[term_class_num] ;
    163   static to_chars_func_t to_chars_funcs[term_class_num] ;
    164160
    165161  void ctor_sym (Term const& _t) ;
     
    175171  pxx::WString to_string_ref () const ;
    176172  pxx::WString to_string_obj () const ;
    177   size_t get_char_len_sym () const ;
    178   size_t get_char_len_ref () const ;
    179   size_t get_char_len_obj () const ;
    180   Term* to_chars_sym (Term* _p) const ;
    181   Term* to_chars_ref (Term* _p) const ;
    182   Term* to_chars_obj (Term* _p) const ;
    183173
    184174public:
     
    229219  /// that this method doesn't check whether a term is a reference term.
    230220  inline MemoryChunk* get_mem_chunk() const ;
    231   ///
    232   /// Get length of the term print representation.
    233   inline size_t get_char_len() const ;
    234   ///
    235   /// Write the term print representation starting with specified adress.
    236   inline Term* to_chars(Term* _p) const ;
    237   ///
    238   /// Get length of the print representation of a term sequence.
    239   static inline size_t get_char_lens(Term const* _p, Term const* _q) ;
     221
     222  inline operator pxx::WString () const ;
    240223
    241224#if defined(RF_STORE_CHUNK_ORDER_IN_TERM)
  • to-imperative/trunk/runtime/rf_term.ih

    r561 r723  
    204204
    205205
    206 inline Term::operator pxx::WString ()
     206inline Term::operator pxx::WString () const
    207207{
    208208  return (this->*to_string_funcs[get_class()])();
    209209}
    210210
    211 inline size_t Term::get_char_len() const
    212 {
    213   return (this->*get_char_len_funcs[get_class()])();
    214 }
    215 
    216 inline Term* Term::to_chars(Term* _p) const
    217 {
    218   return (this->*to_chars_funcs[get_class()])(_p);
    219 }
    220 
    221 inline size_t Term::get_char_lens(Term const* _p, Term const* _q)
    222 {
    223   size_t len = 0;
    224 
    225   for (; _p < _q; _p++)
    226   {
    227     size_t term_char_len = _p->get_char_len();
    228     if (len > SIZE_MAX - term_char_len)
    229       throw Expr::TooLargeArgument();
    230     else
    231       len += term_char_len;
    232   }
    233 
    234   return len;
    235 }
    236 
    237211}
    238212
  • to-imperative/trunk/runtime/rf_types.hh

    r705 r723  
    5252  /// Conversion to string.
    5353  pxx::WString (*to_string)(uintptr_t) ;
    54   ///
    55   /// Number of characters in the print representation.
    56   size_t (*get_char_len)(uintptr_t) ;
    57   ///
    58   /// Write the print representation starting with specified adress.
    59   Term* (*to_chars)(uintptr_t, Term*) ;
    6054} short_type_funcs_t ;
    6155
  • to-imperative/trunk/runtime/rf_word.cc

    r705 r723  
    6363  Word::compare,
    6464  Word::hash,
    65   Word::to_string,
    66   Word::get_char_len,
    67   Word::to_chars
     65  Word::to_string
    6866};
    6967
  • to-imperative/trunk/runtime/rf_word.hh

    r705 r723  
    5050  static uint32_t hash (uintptr_t _data) ;
    5151  static pxx::WString to_string (uintptr_t _data) ;
    52   static size_t get_char_len (uintptr_t _data) ;
    53   static Term* to_chars (uintptr_t _data, Term* _p) ;
    5452  static short_type_funcs_t funcs ;
    5553  static TypeRegister reg ;
  • to-imperative/trunk/samples/Convert/tochars.rf

    r690 r723  
    22$use StdIO;
    33
    4 Main = <PrintLN <To-Chars 0123 5 'abc' 678 '9 10'>>;
     4Main =
     5  <PrintLN <To-Chars 0123 5 'abc' 678 '9 10'>>,
     6  <PrintLN <To-Chars ()> <To-Chars /*empty*/> <To-Chars ()>>,
     7  <PrintLN <To-Chars ((())) (()) ()>>,
     8  <PrintLN <To-Chars 123 (456 (789) 456) 123>>;
Note: See TracChangeset for help on using the changeset viewer.