StringClassBit.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* REXX Kernel StringClassBit.cpp */
40 /* */
41 /* REXX string BITxxx methods */
42 /* */
43 /******************************************************************************/
44 
45 #include <ctype.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <math.h>
49 #include "RexxCore.h"
50 #include "StringClass.hpp"
51 #include "ProtectedObject.hpp"
52 
53 /******************************************************************************/
54 /* Arguments: String to bitand with self */
55 /* pad character to use */
56 /* */
57 /* Returned: New string object */
58 /******************************************************************************/
59 // in behaviour
61  RexxString *pad)
62 {
63  char PadChar; /* pad character */
64  const char *String1; /* string 1 pointer */
65  const char *PadString; /* padded string part */
66  const char *String2; /* string 2 pointer */
67  sizeB_t String1Len; /* string 1 length */
68  sizeB_t String2Len; /* string 2 length */
69  sizeB_t MinLength; /* length of shorter string */
70  sizeB_t PadLength; /* length to pad */
71  sizeB_t MaxLength; /* longest length */
72  RexxString *Retval; /* return value */
73  const char *Source; /* source string pointer */
74  char *Target; /* target string pointer */
75 
76  /* get string we will be doing bit */
77  /* stuff to... */
78  string2 = optionalStringArgument(string2, OREF_NULLSTRING, OREF_positional, ARG_ONE);
79  ProtectedObject p(string2);
80  String2Len = string2->getBLength(); /* get the string length */
81  String2 = string2->getStringData(); /* get the string data pointer */
82  /* get the pad character */
83  PadChar = optionalPadArgument(pad, (char)0xff, ARG_TWO);
84 
85  String1 = this->getStringData(); /* point to the first string */
86  String1Len = this->getBLength(); /* get the length */
87  if (String1Len <= String2Len)
88  { /* string 1 shorter or equal? */
89  MinLength = String1Len; /* string 1 is the shorter */
90  MaxLength = String2Len; /* string 2 is the longer */
91  PadString = String2; /* padding is done on string2 */
92  Source = String1; /* operate from string 1 */
93  }
94  else
95  {
96  MinLength = String2Len; /* string 2 is the shorter */
97  MaxLength = String1Len; /* string 1 is the longer */
98  PadString = String1; /* padding is done on string1 */
99  Source = String2; /* operate from string 2 */
100  }
101  PadLength = MaxLength - MinLength; /* get the padding length */
102  /* Duplicate Longer */
103  Retval = raw_string(MaxLength);
104  Target = Retval->getWritableData(); /* point to the tArget */
105  memcpy(Target, PadString, MaxLength);/* now copy in the longer one */
106 
107  while (MinLength-- != 0)
108  { /* while shorter has data */
109  /* and in each character */
110  *Target = *Target & *Source++;
111  Target++; /* step the target */
112  }
113 
114  while (PadLength-- != 0)
115  { /* while pad needed */
116  /* and in a pad character */
117  *Target = *Target & PadChar;
118  Target++; /* step the target */
119  }
120  return Retval; /* return result string */
121 }
122 
123 /* the BITOR function */
124 /******************************************************************************/
125 /* Arguments: String to bitor with self */
126 /* pad character to use */
127 /* */
128 /* Returned: New string object */
129 /******************************************************************************/
130 // in behaviour
132  RexxString *pad)
133 {
134  char PadChar; /* pad character */
135  const char *String1; /* string 1 pointer */
136  const char *PadString; /* padded string part */
137  const char *String2; /* string 2 pointer */
138  sizeB_t String1Len; /* string 1 length */
139  sizeB_t String2Len; /* string 2 length */
140  sizeB_t MinLength; /* length of shorter string */
141  sizeB_t PadLength; /* length to pad */
142  sizeB_t MaxLength; /* longest length */
143  RexxString *Retval; /* return value */
144  const char *Source; /* source string pointer */
145  char *Target; /* tArget string pointer */
146 
147  /* get string we will be doing bit */
148  /* stuff to... */
149  string2 = optionalStringArgument(string2, OREF_NULLSTRING, OREF_positional, ARG_ONE);
150  ProtectedObject p(string2);
151  String2Len = string2->getBLength(); /* get the string length */
152  String2 = string2->getStringData(); /* get the string data pointer */
153  /* get the pad character */
154  PadChar = optionalPadArgument(pad, 0x00, ARG_TWO);
155 
156  String1 = this->getStringData(); /* point to the first string */
157  String1Len = this->getBLength(); /* get the length */
158  if (String1Len <= String2Len)
159  { /* string 1 shorter or equal? */
160  MinLength = String1Len; /* string 1 is the shorter */
161  MaxLength = String2Len; /* string 2 is the longer */
162  PadString = String2; /* padding is done on string2 */
163  Source = String1; /* operate from string 1 */
164  }
165  else
166  {
167  MinLength = String2Len; /* string 2 is the shorter */
168  MaxLength = String1Len; /* string 1 is the longer */
169  PadString = String1; /* padding is done on string1 */
170  Source = String2; /* operate from string 2 */
171  }
172  PadLength = MaxLength - MinLength; /* get the padding length */
173  /* Duplicate Longer */
174  Retval = raw_string(MaxLength);
175  Target = Retval->getWritableData(); /* point to the tArget */
176  memcpy(Target, PadString, MaxLength);/* now copy in the longer one */
177 
178  while (MinLength-- != 0)
179  { /* while shorter has data */
180  /* and in each character */
181  *Target = *Target | *Source++;
182  Target++; /* step the target */
183  }
184 
185  while (PadLength-- != 0)
186  { /* while pad needed */
187  /* and in a pad character */
188  *Target = *Target | PadChar;
189  Target++; /* step the target */
190  }
191  return Retval; /* return result string */
192 }
193 
194 /* the BITXOR function */
195 /******************************************************************************/
196 /* Arguments: String to bitxor with self */
197 /* pad character to use */
198 /* */
199 /* Returned: New string object */
200 /******************************************************************************/
201 // in behaviour
203  RexxString *pad)
204 {
205  char PadChar; /* pad character */
206  const char *String1; /* string 1 pointer */
207  const char *PadString; /* padded string part */
208  const char *String2; /* string 2 pointer */
209  sizeB_t String1Len; /* string 1 length */
210  sizeB_t String2Len; /* string 2 length */
211  sizeB_t MinLength; /* length of shorter string */
212  sizeB_t PadLength; /* length to pad */
213  sizeB_t MaxLength; /* longest length */
214  RexxString *Retval; /* return value */
215  const char *Source; /* source string pointer */
216  char *Target; /* tArget string pointer */
217 
218  /* get string we will be doing bit */
219  /* stuff to... */
220  string2 = optionalStringArgument(string2, OREF_NULLSTRING, OREF_positional, ARG_ONE);
221  ProtectedObject p(string2);
222  String2Len = string2->getBLength(); /* get the string length */
223  String2 = string2->getStringData(); /* get the string data pointer */
224  /* get the pad character */
225  PadChar = optionalPadArgument(pad, 0x00, ARG_TWO);
226 
227  String1 = this->getStringData(); /* point to the first string */
228  String1Len = this->getBLength(); /* get the length */
229  if (String1Len <= String2Len)
230  { /* string 1 shorter or equal? */
231  MinLength = String1Len; /* string 1 is the shorter */
232  MaxLength = String2Len; /* string 2 is the longer */
233  PadString = String2; /* padding is done on string2 */
234  Source = String1; /* operate from string 1 */
235  }
236  else
237  {
238  MinLength = String2Len; /* string 2 is the shorter */
239  MaxLength = String1Len; /* string 1 is the longer */
240  PadString = String1; /* padding is done on string1 */
241  Source = String2; /* operate from string 2 */
242  }
243  PadLength = MaxLength - MinLength; /* get the padding length */
244  /* Duplicate Longer */
245  Retval = raw_string(MaxLength);
246  Target = Retval->getWritableData(); /* point to the tArget */
247  memcpy(Target, PadString, MaxLength);/* now copy in the longer one */
248 
249  while (MinLength-- != 0)
250  { /* while shorter has data */
251  /* and in each character */
252  *Target = *Target ^ *Source++;
253  Target++; /* step the target */
254  }
255 
256  while (PadLength-- != 0)
257  { /* while pad needed */
258  /* and in a pad character */
259  *Target = *Target ^ PadChar;
260  Target++; /* step the target */
261  }
262  return Retval; /* return result string */
263 }
264 
codepoint_t optionalPadArgument(RexxObject *o, codepoint_t d, size_t p)
Definition: RexxCore.h:382
const int ARG_TWO
Definition: RexxCore.h:81
const int ARG_ONE
Definition: RexxCore.h:80
RexxString * optionalStringArgument(RexxObject *o, RexxString *d, RexxString *kind, size_t p)
Definition: RexxCore.h:328
RexxString * raw_string(stringsizeB_t bl, stringsizeC_t cl=-1)
RexxString * bitOr(RexxString *, RexxString *)
RexxString * bitAnd(RexxString *, RexxString *)
const char * getStringData()
RexxString * bitXor(RexxString *, RexxString *)
char * getWritableData()
sizeB_t getBLength()
stringsizeB_t sizeB_t
Definition: rexx.h:248