ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
string2.cpp
Go to the documentation of this file.
1 
8 #include <fvar.hpp>
9 #include <string.h>
10 #include <stdlib.h>
11 
12 #ifndef OPT_LIB
13  #include <cassert>
14 #endif
15 
16 adstring::adstring(const size_t lb, const size_t ub) : clist()
17 {
18  if (lb != 1)
19  {
20  cerr << " Error in adstring::adstring(int lb,int ub) : clist()\n"
21  " At present minimum index for adstring must equal 1\n";
22  ad_exit(1);
23  }
24  size_t sz = ub;
25  allocate(sz);
26  for (size_t i = 1; i <= sz; i++)
27  {
28  s[i] = '\0'; //' ';
29  }
30  s[sz + 1] = '\0';
31 }
32 adstring::adstring(const int sz) : clist()
33 {
34  if (sz < 1)
35  {
36  cerr << " Error in adstring::adstring(const size_t size) : clist()\n"
37  " At present size must be greater than zero.\n";
38  ad_exit(1);
39  }
40  size_t size = sz > 0 ? (size_t)sz : 0;
41  allocate(size);
42  for (size_t i = 1; i <= size; i++)
43  {
44  s[i] = '\0'; //' ';
45  }
46  s[size + 1] = '\0';
47 }
48 
49 adstring::adstring(const char ub) : clist()
50 {
51  size_t sz = 1;
52  allocate(sz);
53 
54  s[1] = (unsigned char)ub;
55  s[2] = '\0';
56 }
57 
58 adstring::adstring(const unsigned char ub) : clist()
59 {
60  size_t sz = 1;
61  allocate(sz);
62 
63  s[1] = ub;
64  s[2] = '\0';
65 }
66 
68 {
69  shape = v.shape;
70  s = v.s;
71 }
72 
73 adstring operator+(const adstring& u, const adstring& v)
74 {
75  size_t us = u.size();
76  size_t vs = v.size();
77  adstring tmp(1, us + vs);
78  for (size_t i = 1; i <= us; i++)
79  {
80  tmp(i) = u(i);
81  }
82  for (size_t i = 1; i <= vs; i++)
83  {
84  tmp(i + us) = v(i);
85  }
86  return tmp;
87 }
88 
89 adstring itoa(int n, int r)
90 {
91 #ifndef OPT_LIB
92  assert(r != 0);
93  assert(r != 1);
94 #endif
95 
96  bool sign = n < 0 ? true : false;
97  if (sign) n *= -1;
98  unsigned char buf[50];
99  memset(&buf[0], ' ', 50);
100  size_t ii=0;
101  do
102  {
103  int nr = n % r;
104  if (nr < 0) nr *= -1;
105  buf[ii++] = (unsigned char)nr;
106  } while (n /= r);
107  if (sign)
108  {
109  ii++;
110  }
111  adstring s(1, ii);
112  for (size_t i=0;i<ii;i++)
113  {
114  s[ii-i]=(unsigned char)(buf[i]+48);
115  }
116  if (sign)
117  {
118  s(1) = '-';
119  }
120  return s;
121 }
size_t size() const
Definition: string.cpp:58
adstring_shape * shape
Definition: adstring.hpp:72
void allocate(const size_t sz)
Definition: string.cpp:12
d3_array operator+(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements addition of a(i, j, k) + b(i, j, k).
Definition: d3arr2a.cpp:132
exitptr ad_exit
Definition: gradstrc.cpp:53
adstring itoa(int n, int d)
Definition: string2.cpp:89
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
adstring()
Default constructor.
Definition: string3.cpp:33
Definition: clist.h:43
unsigned char * s
Definition: adstring.hpp:76
double sign(const double x)
The sign of a number.
Definition: gaussher.cpp:25