ooRexx  4.2.0-source
About: ooRexx (Open Object Rexx) is a free implementation of Object Rexx. Object Rexx is an enhancement of the classic Rexx interpreter; a full-featured programming language with a human-oriented syntax.
  Fossies Dox: ooRexx-4.2.0-source.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

StringClassWord.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 */
40 /* */
41 /* Word-related REXX string 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 "StringUtil.hpp"
52 
53 
54 /* the DELWORD function */
55 /******************************************************************************/
56 /* Arguments: nth word to start deleting */
57 /* number of words to delete */
58 /* */
59 /* Returned: string, with length words deleted */
60 /******************************************************************************/
62  RexxInteger *plength)
63 {
64  char *Current; /* current pointer position */
65  const char *Word; /* current word pointer */
66  const char *NextSite; /* next word */
67  size_t WordPos; /* needed word position */
68  size_t Count; /* count of words */
69  size_t Length; /* remaining length */
70  size_t WordLength; /* word size */
71  size_t FrontLength; /* front substring */
72  RexxString *Retval; /* return value */
73 
74  /* convert position to binary */
75  WordPos = positionArgument(position, ARG_ONE);
76  /* get num of words to delete, the */
77  /* default is "a very large number" */
79 
80  Length = this->getLength(); /* get string length */
81  if (!Length) /* null string? */
82  {
83  Retval = OREF_NULLSTRING; /* result is null also */
84  }
85  else if (!Count) /* deleting zero words? */
86  {
87  Retval = this; /* just use this string */
88  }
89  else
90  {
91  Word = this->getStringData(); /* point to the string */
92  /* get the first word */
93  WordLength = StringUtil::nextWord(&Word, &Length, &NextSite);
94  while (--WordPos && WordLength)
95  { /* loop until we reach tArget */
96  Word = NextSite; /* copy the start pointer */
97  /* get the next word */
98  WordLength = StringUtil::nextWord(&Word, &Length, &NextSite);
99  }
100  if (WordPos) /* run out of words first */
101  {
102  Retval = this; /* return entire string */
103  }
104  else
105  { /* count off number of words */
106  /* calculate front length */
107  FrontLength = (size_t)(Word - this->getStringData());
108  while (--Count && WordLength)
109  { /* loop until we reach tArget */
110  Word = NextSite; /* copy the start pointer */
111  /* get the next word */
112  WordLength = StringUtil::nextWord(&Word, &Length, &NextSite);
113  }
114  if (Length) /* didn't use up the string */
115  {
116  StringUtil::skipBlanks(&NextSite, &Length);/* skip over trailing blanks */
117  }
118  /* allocate return string */
119  Retval = raw_string(FrontLength + Length);
120  /* point to data portion */
121  Current = Retval->getWritableData();
122  if (FrontLength)
123  { /* have a leading portion? */
124  /* copy into the result */
125  memcpy(Current, this->getStringData(), FrontLength);
126  Current += FrontLength; /* step output position */
127  }
128  if (Length) /* any string left? */
129  {
130  /* copy what's left */
131  memcpy(Current, NextSite, Length);
132  }
133  }
134  }
135  return Retval; /* return deleted string */
136 }
137 
138 /* the SPACE function */
139 /******************************************************************************/
140 /* Arguments: number of pad characters between each word */
141 /* pad character */
142 /* */
143 /* Returned: string */
144 /******************************************************************************/
146  RexxString *pad)
147 {
148  size_t Spaces; /* requested spacing */
149  char PadChar; /* pad character */
150  char *Current; /* current pointer position */
151  const char *Word; /* current word pointer */
152  const char *NextSite; /* next word */
153  size_t Count; /* count of words */
154  size_t WordSize; /* size of words */
155  size_t Length; /* remaining length */
156  size_t WordLength; /* word size */
157  RexxString *Retval; /* return value */
158 
159  /* get the spacing count */
160  Spaces = optionalLengthArgument(space_count, 1, ARG_ONE);
161 
162  /* get the pad character */
163  PadChar = optionalPadArgument(pad, ' ', ARG_TWO);
164 
165  Length = this->getLength(); /* get the string length */
166  Count = 0; /* no words yet */
167  WordSize = 0; /* no characters either */
168  Word = this->getStringData(); /* point to the string */
169  /* get the first word */
170  WordLength = StringUtil::nextWord(&Word, &Length, &NextSite);
171 
172  while (WordLength)
173  { /* loop until we reach tArget */
174  Count++; /* count the word */
175  WordSize += WordLength; /* add in the word length */
176  Word = NextSite; /* copy the start pointer */
177  /* get the next word */
178  WordLength = StringUtil::nextWord(&Word, &Length, &NextSite);
179  }
180  if (!Count) /* no words? */
181  {
182  Retval = OREF_NULLSTRING; /* this is a null string */
183  }
184  else
185  { /* real words */
186  Count--; /* step back one */
187  /* get space for output */
188  Retval = raw_string(WordSize + Count * Spaces);
189  /* point to output area */
190  Current = Retval->getWritableData();
191 
192  Length = this->getLength(); /* recover the length */
193  Word = this->getStringData(); /* point to the string */
194  /* get the first word */
195  WordLength = StringUtil::nextWord(&Word, &Length, &NextSite);
196 
197  while (Count--)
198  { /* loop for each word */
199  /* copy the word over */
200  memcpy(Current, Word, WordLength);
201  Current += WordLength; /* step over the word */
202  if (Spaces)
203  { /* if have gaps... */
204  /* fill in the pad chars */
205  memset(Current, PadChar, Spaces);
206  Current += Spaces; /* step over the pad chars */
207  }
208  Word = NextSite; /* copy the start pointer */
209  /* get the next word */
210  WordLength = StringUtil::nextWord(&Word, &Length, &NextSite);
211  }
212  /* copy the word over */
213  memcpy(Current, Word, (size_t)WordLength);
214  }
215  return Retval; /* return spaced string */
216 }
217 
218 
219 /* the SUBWORD function */
220 /******************************************************************************/
221 /* Arguments: Starting word postion */
222 /* number of words */
223 /* */
224 /* Returned: string, contains the requested number of words from source */
225 /******************************************************************************/
227 {
228  return StringUtil::subWord(getStringData(), getLength(), position, plength);
229 }
230 
231 
247 {
248  return StringUtil::subWords(getStringData(), getLength(), position, plength);
249 }
250 
251 
252 /* the WORD function */
253 /******************************************************************************/
254 /* Arguments: which word we want. */
255 /* */
256 /* Returned: string, containing nth word. */
257 /******************************************************************************/
259 {
260  return StringUtil::word(getStringData(), getLength(), position);
261 }
262 
263 /* the WORDINDEX function */
264 /******************************************************************************/
265 /* Arguments: word we want position of. */
266 /* */
267 /* Returned: integer, actual char position of nth word */
268 /******************************************************************************/
270 {
271  return StringUtil::wordIndex(getStringData(), getLength(), position);
272 }
273 
274 /* the WORDLENGTH function */
275 /******************************************************************************/
276 /* Arguments: nth word we want length of */
277 /* */
278 /* Returned: integer, length of nth word */
279 /******************************************************************************/
281 {
282  return StringUtil::wordLength(getStringData(), getLength(), position);
283 }
284 
285 
295 {
296  return StringUtil::wordPos(getStringData(), getLength(), phrase, pstart);
297 }
298 
299 
309 {
310  return StringUtil::caselessWordPos(getStringData(), getLength(), phrase, pstart);
311 }
312 
313 /* the WORDS function */
314 /******************************************************************************/
315 /* Arguments: none */
316 /* */
317 /* Returned: integer, number os words in source */
318 /******************************************************************************/
320 {
321  size_t tempCount = StringUtil::wordCount(this->getStringData(), this->getLength());
322  return new_integer(tempCount);
323 }
324 
325 
StringUtil.hpp
RexxString::caselessWordPos
RexxInteger * caselessWordPos(RexxString *, RexxInteger *)
Definition: StringClassWord.cpp:308
RexxArray
Definition: ArrayClass.hpp:100
RexxString::space
RexxString * space(RexxInteger *, RexxString *)
Definition: StringClassWord.cpp:145
StringUtil::caselessWordPos
static RexxInteger * caselessWordPos(const char *data, size_t length, RexxString *phrase, RexxInteger *pstart)
Definition: StringUtil.cpp:1844
RexxString::words
RexxInteger * words()
Definition: StringClassWord.cpp:319
new_integer
RexxInteger * new_integer(wholenumber_t v)
Definition: IntegerClass.hpp:198
optionalLengthArgument
size_t optionalLengthArgument(RexxObject *o, size_t d, size_t p)
Definition: RexxCore.h:336
StringUtil::wordIndex
static RexxInteger * wordIndex(const char *data, size_t length, RexxInteger *position)
Definition: StringUtil.cpp:1675
RexxString::getLength
size_t getLength()
Definition: StringClass.hpp:330
StringUtil::skipBlanks
static void skipBlanks(const char **String, size_t *StringLength)
Definition: StringUtil.cpp:1214
StringUtil::wordCount
static size_t wordCount(const char *String, size_t StringLength)
Definition: StringUtil.cpp:1273
ARG_TWO
const int ARG_TWO
Definition: RexxCore.h:81
RexxString::getWritableData
char * getWritableData()
Definition: StringClass.hpp:334
Numerics::MAX_WHOLENUMBER
static const wholenumber_t MAX_WHOLENUMBER
Definition: Numerics.hpp:62
StringUtil::word
static RexxString * word(const char *data, size_t length, RexxInteger *position)
Definition: StringUtil.cpp:1608
RexxString::getStringData
const char * getStringData()
Definition: StringClass.hpp:333
StringUtil::subWord
static RexxString * subWord(const char *data, size_t length, RexxInteger *position, RexxInteger *plength)
Definition: StringUtil.cpp:1496
StringUtil::wordLength
static RexxInteger * wordLength(const char *data, size_t length, RexxInteger *position)
Definition: StringUtil.cpp:1709
StringUtil::wordPos
static RexxInteger * wordPos(const char *data, size_t length, RexxString *phrase, RexxInteger *pstart)
Definition: StringUtil.cpp:1738
raw_string
RexxString * raw_string(stringsize_t l)
Definition: StringClass.hpp:529
ARG_ONE
const int ARG_ONE
Definition: RexxCore.h:80
RexxString::wordLength
RexxInteger * wordLength(RexxInteger *)
Definition: StringClassWord.cpp:280
RexxString::wordPos
RexxInteger * wordPos(RexxString *, RexxInteger *)
Definition: StringClassWord.cpp:294
StringClass.hpp
RexxString::subWords
RexxArray * subWords(RexxInteger *, RexxInteger *)
Definition: StringClassWord.cpp:246
optionalPadArgument
char optionalPadArgument(RexxObject *o, char d, size_t p)
Definition: RexxCore.h:351
RexxString::word
RexxString * word(RexxInteger *)
Definition: StringClassWord.cpp:258
RexxString::delWord
RexxString * delWord(RexxInteger *, RexxInteger *)
Definition: StringClassWord.cpp:61
StringUtil::nextWord
static size_t nextWord(const char **String, size_t *StringLength, const char **NextString)
Definition: StringUtil.cpp:1308
StringUtil::subWords
static RexxArray * subWords(const char *data, size_t length, RexxInteger *position, RexxInteger *plength)
Definition: StringUtil.cpp:1550
RexxString::subWord
RexxString * subWord(RexxInteger *, RexxInteger *)
Definition: StringClassWord.cpp:226
RexxString::wordIndex
RexxInteger * wordIndex(RexxInteger *)
Definition: StringClassWord.cpp:269
positionArgument
stringsize_t positionArgument(RexxObject *argument, size_t position)
Definition: StringClassUtil.cpp:82
RexxCore.h
RexxInteger
Definition: IntegerClass.hpp:56
RexxString
Definition: StringClass.hpp:119