Exemplo n.º 1
0
// in behaviour
RexxString *RexxString::bitAnd(RexxString *string2,
                               RexxString *pad)
{
    char        PadChar;                 /* pad character                     */
    const char *String1;                 /* string 1 pointer                  */
    const char *PadString;               /* padded string part                */
    const char *String2;                 /* string 2 pointer                  */
    sizeB_t      String1Len;              /* string 1 length                   */
    sizeB_t      String2Len;              /* string 2 length                   */
    sizeB_t      MinLength;               /* length of shorter string          */
    sizeB_t      PadLength;               /* length to pad                     */
    sizeB_t      MaxLength;               /* longest length                    */
    RexxString *Retval;                  /* return value                      */
    const char *Source;                  /* source string pointer             */
    char       *Target;                  /* target string pointer             */

                                         /* get string we will be doing bit   */
                                         /* stuff to...                       */
    string2 = optionalStringArgument(string2, OREF_NULLSTRING, ARG_ONE);
    ProtectedObject p(string2);
    String2Len = string2->getBLength();        /* get the string length             */
    String2 = string2->getStringData();       /* get the string data pointer       */
    /* get the pad character             */
    PadChar = optionalPadArgument(pad, (char)0xff, ARG_TWO);

    String1 = this->getStringData();     /* point to the first string         */
    String1Len = this->getBLength();      /* get the length                    */
    if (String1Len <= String2Len)
    {      /* string 1 shorter or equal?        */
        MinLength = String1Len;            /* string 1 is the shorter           */
        MaxLength = String2Len;            /* string 2 is the longer            */
        PadString = String2;               /* padding is done on string2        */
        Source = String1;                  /* operate from string 1             */
    }
    else
    {
        MinLength = String2Len;            /* string 2 is the shorter           */
        MaxLength = String1Len;            /* string 1 is the longer            */
        PadString = String1;               /* padding is done on string1        */
        Source = String2;                  /* operate from string 2             */
    }
    PadLength = MaxLength - MinLength;   /* get the padding length            */
                                         /* Duplicate Longer                  */
    Retval = raw_string(MaxLength);
    Target = Retval->getWritableData();  /* point to the tArget               */
    memcpy(Target, PadString, MaxLength);/* now copy in the longer one        */

    while (MinLength-- != 0)
    {                /* while shorter has data            */
                     /* and in each character             */
        *Target = *Target & *Source++;
        Target++;                          /* step the target                   */
    }

    while (PadLength-- != 0)
    {                /* while pad needed                  */
                     /* and in a pad character            */
        *Target = *Target & PadChar;
        Target++;                          /* step the target                   */
    }
    return Retval;                       /* return result string              */
}
Exemplo n.º 2
0
// in behaviour
RexxString *RexxString::space(RexxInteger *space_count,
                              RexxString  *pad)
{
    sizeC_t      Spaces;                  /* requested spacing                 */
    codepoint_t        PadChar;                 /* pad character                     */
    char       *Current;                 /* current pointer position          */
    const char *Word;                    /* current word pointer              */
    const char *NextSite;                /* next word                         */
    sizeB_t      Count;                   /* count of words                    */
    sizeB_t      WordSize;                /* size of words                     */
    sizeB_t      Length;                  /* remaining length                  */
    sizeB_t      WordLength;              /* word size                         */
    RexxString *Retval;                  /* return value                      */

                                         /* get the spacing count             */
    Spaces = optionalLengthArgument(space_count, 1, ARG_ONE);

    /* get the pad character             */
    PadChar = optionalPadArgument(pad, ' ', ARG_TWO);

    Length = this->getBLength();               /* get the string length             */
    Count = 0;                           /* no words yet                      */
    WordSize = 0;                        /* no characters either              */
    Word = this->getStringData();        /* point to the string               */
                                         /* get the first word                */
    WordLength = StringUtil::nextWord(&Word, &Length, &NextSite);

    while (WordLength != 0)
    {                 /* loop until we reach tArget        */
        Count++;                           /* count the word                    */
        WordSize += WordLength;            /* add in the word length            */
        Word = NextSite;                   /* copy the start pointer            */
                                           /* get the next word                 */
        WordLength = StringUtil::nextWord(&Word, &Length, &NextSite);
    }
    if (Count == 0)                          /* no words?                         */
    {
        Retval = OREF_NULLSTRING;          /* this is a null string             */
    }
    else
    {                               /* real words                        */
        Count--;                           /* step back one                     */
                                           /* get space for output              */
        Retval = raw_string(WordSize + Count * size_v(Spaces)); // todo m17n : Spaces is a char count
        /* point to output area              */
        Current = Retval->getWritableData();

        Length = this->getBLength();             /* recover the length                */
        Word = this->getStringData();      /* point to the string               */
                                           /* get the first word                */
        WordLength = StringUtil::nextWord(&Word, &Length, &NextSite);

        while (Count-- != 0)
        {                  /* loop for each word                */
                           /* copy the word over                */
            memcpy(Current, Word, WordLength);
            Current += WordLength;           /* step over the word                */
            if (Spaces != 0)
            {                    /* if have gaps...                   */
                                 /* fill in the pad chars             */
                memset(Current, PadChar, size_v(Spaces)); // todo m17n
                Current += size_v(Spaces);             /* step over the pad chars           */ // todo m17n
            }
            Word = NextSite;                 /* copy the start pointer            */
                                             /* get the next word                 */
            WordLength = StringUtil::nextWord(&Word, &Length, &NextSite);
        }
        /* copy the word over                */
        memcpy(Current, Word, WordLength);
    }
    return Retval;                       /* return spaced string              */
}