      *>----------------------------------------------------------------
      *> A ByteArray stores strings as objects, and is a convenient
      *> way to handle text of any length. A ByteArray also
      *> provides methods for manipulating and searching itself.
      *>
      *> The CobolPicx class provides the same functionality as
      *> ByteArray, but ByteArrays are more efficient and
      *> easier to use when creating instances. This is because
      *> a ByteArray stores the string as indexed data, and you
      *> don't need to clone the ByteArray class to the length of
      *> data you want to store. Use instances of
      *> Cobolpicx only when it is important that the instance data
      *> comprises just the string.
      *>
      *>#See Using ByteArrays.
      *>
      *>#See class "picx".
      *>
      *> Copyright (C) 1991-1999 Micro Focus International Ltd.
      *>
      *>----------------------------------------------------------------
      $set mfoo linkcount(255) localcount(255)
       class-id. ByteArray data is protected
               inherits from array with data.


        object section.
        class-control.
           array is class "array"
           cobolpicx is class "picx"
           bytearray is class "bytearry"
           CobolCOMP5 is class "comp5"
           ValueSet is class "valueset"
           DBCSCharacterArray is class "dbcsarry"
           Orderedcollection is class "ordrdcll".

       working-storage section.
       01 dbcsEnvironmentStatus    pic x comp-5 value 1.
           88  isSBCSEnv value 1.
           88  isDBCSEnv value 2.

       class-object.

       object-storage section.

      *>----------------------------------------------------------------
      *> Raises an exception as "ofReferences" is not a valid message
      *> for ByteArray. A ByteArray can only consist of
      *> actual characters, rather than references to characters.
      *>----------------------------------------------------------------
       method-id. "ofReferences".
       linkage section.
       01 lnksize           pic s9(9) comp-5.
       01 lnkobject         usage object reference.

       procedure division using lnksize returning lnkobject.
           invoke self "shouldNotImplement" returning lnkobject
           exit method.
       end method "ofReferences".
      *>----------------------------------------------------------------
      *> Returns a new ByteArray instance of length lnkSize and
      *> value lnkValue. Although lnkValue here is specified as PIC X,
      *> you can pass in a string of any length. Specify lnkSize in
      *> bytes.
      *>----------------------------------------------------------------
       method-id. "withLengthValue".
       linkage section.
       01 lnksize           pic s9(9) comp-5.
       01 lnkobject         usage object reference.
       01 lnkvalue          pic x.

       procedure division using lnksize lnkvalue
                            returning lnkobject.
           invoke self "new" using lnkSize returning lnkobject
           invoke lnkobject "setValue" using lnkvalue
           exit method.
       end method "withLengthValue".


      *>----------------------------------------------------------------
      *> Returns a new ByteArray instance from the data in buffer
      *> lnkValue. The length of the data is calculated by searching
      *> backwards from the end of the buffer and finding the last
      *> non-space and non-null character.
      *> Specify the buffer size in bytes in lnkSize.
      *>----------------------------------------------------------------
       method-id. "fromBuffer".
       local-storage section.
       01 i                 pic s9(9) comp-5.
       01 lsBool            pic x comp-5.
       linkage section.
       01 lnksize           pic s9(9) comp-5.
       01 lnkobject         usage object reference.
       01 lnkvalue          pic x.

       procedure division using lnksize lnkvalue
                            returning lnkobject.
           if not isSBCSEnv
               invoke DBCSCHaracterArray "isDBCSStringByteLengthValue"
                       using lnkSize lnkValue
                   returning lsBool
           end-if
           if isSBCSEnv or lsBool = 0
               perform varying i from lnkSize by -1 until i < 1 or
                 (lnkValue(i:1) not = ' ' and lnkValue(i:1) not = x"00")
               end-perform
               invoke self "withLengthValue" using i lnkValue
                                     returning lnkobject
           else
               invoke DBCSCHaracterArray "fromBuffer"
                       using lnkSize lnkValue
                   returning lsBool
           end-if
           exit method.
       end method "fromBuffer".

      *>----------------------------------------------------------------
      *> Returns a new ByteArray instance of length lnkSize and
      *> value lnkValue. Although lnkValue here is specified as PIC X,
      *> you can pass in a string of any length. Specify lnkSize in
      *> bytes.
      *>----------------------------------------------------------------
       method-id. "withByteLengthValue".
       local-storage section.
       01 lsBool   pic x comp-5.

       linkage section.
       01 lnksize           pic s9(9) comp-5.
       01 lnkobject         usage object reference.
       01 lnkvalue          pic x.

       procedure division using lnksize lnkvalue
                            returning lnkobject.
           if isSBCSEnv
               invoke self "withLengthValue" using lnkSize lnkValue
                                     returning lnkobject
           else
               invoke DBCSCHaracterArray "isDBCSStringByteLengthValue"
                       using lnkSize lnkValue
                   returning lsBool
               if lsBool = 0
                   invoke self "withLengthValue" using lnkSize lnkValue
                                     returning lnkobject
               else
                   invoke DBCSCHaracterArray "withByteLengthValue"
                                         using lnkSize lnkValue
                                     returning lnkobject
               end-if
           end-if
           exit method.
       end method "withByteLengthValue".

      *>----------------------------------------------------------------
      *> Returns a new ByteArray instance of length lnkSize. The
      *> contents are undefined. Specify lnkSize in bytes.
      *>----------------------------------------------------------------
       method-id. "withLength".
       linkage section.
       01 lnksize           pic s9(9) comp-5.
       01 lnkobject         usage object reference.

       procedure division using lnksize returning lnkobject.
           invoke self "new" using lnkSize returning lnkobject
           exit method.
       end method "withLength".

      *>----------------------------------------------------------------
      *> Returns an instance of ByteArray with the value of
      *> null-terminated string lnkString.
      *>----------------------------------------------------------------
       method-id. "withValue".
       local-storage section.
       01 i        pic x(4) comp-5.
       01 aString  usage object reference.
       linkage section.
       01 lnkstring pic x.
       01 lnkObject usage object reference.

       procedure division using lnkstring returning lnkObject.
           if isSBCSEnv
               perform varying i from 1 by 1 until lnkString(i:1) =
                   x"00"
               end-perform
               subtract 1 from i
               invoke self "withLengthValue" using i lnkString
                       returning lnkObject
           else
               invoke DBCSCHaracterArray "withValue" using lnkString
                   returning lnkObject
           end-if
           exit method.
       end method "withValue".

      *>----------------------------------------------------------------
      *> Returns an instance of ByteArray with the value of
      *> null-terminated string lnkString.
      *>----------------------------------------------------------------
       method-id. "withSpaceTerminator".
       local-storage section.
       01 i        pic x(4) comp-5.
       01 aString  usage object reference.
       linkage section.
       01 lnkstring pic x.
       01 lnkObject usage object reference.

       procedure division using lnkstring returning lnkObject.
           if isSBCSEnv
               perform varying i from 1 by 1
                   until lnkString(i:1) = space
               end-perform
               subtract 1 from i
               invoke self "withLengthValue" using i lnkString
                   returning lnkObject
           else
               invoke DBCSCHaracterArray "withSpaceTerminator"
                           using lnkString
                       returning lnkObject
           end-if
           exit method.
       end method "withSpaceTerminator".

      *>----------------------------------------------------------------
      *> Returns an instance of ByteArray with the value of
      *> null or space-terminated string lnkString.
      *>----------------------------------------------------------------
       method-id. "withSpaceOrNullTerminator".
       local-storage section.
       01 i        pic x(4) comp-5.
       01 aString  usage object reference.
       linkage section.
       01 lnkstring pic x.
       01 lnkObject usage object reference.

       procedure division using lnkstring returning lnkObject.
           if isSBCSEnv
               perform varying i from 1 by 1
                   until lnkString(i:1) = space
                   or lnkString(i:1) = x"00"
               end-perform
               subtract 1 from i
               invoke self "withLengthValue" using i lnkString
                       returning lnkObject
           else
               invoke DBCSCHaracterArray "withSpaceOrNullTerminator"
                           using lnkString
                       returning lnkObject
           end-if
           exit method.
       end method "withSpaceOrNullTerminator".
      *>----------------------------------------------------------------
      *> Returns a new ByteArray instance of length lnkSize. The
      *> contents are undefined. Specify lnkSize in bytes.
      *>----------------------------------------------------------------
       method-id. "new".
       local-storage section.
       01 astride              pic s9(9) comp-5.

       linkage section.
       01 lnksize           pic s9(9) comp-5.
       01 lnkobject         usage object reference.

       procedure division using lnksize returning lnkobject.
           if lnksize < zero
               invoke self "invalidParameter" returning lnkobject
           else
               move 1 to astride
               invoke super "new" returning lnkObject
               invoke lnkObject "initialize" using lnkSize
                                                   cobolpicx
                                                   aStride
           end-if
           exit method.
       end method "new".
      *>----------------------------------------------------------------
      *> Overrides the superclass method ofValues to enforce that the
      *> template is CobolPICX. In fact the parameter lnkTemplate is not
      *> used.
      *>----------------------------------------------------------------
       method-id. "ofValues".
       linkage section.
       01 lnksize           pic s9(9) comp-5.
       01 lnktemplate       usage object reference.
       01 lnkobject         usage object reference.

       procedure division using lnktemplate lnksize
                           returning lnkobject.
           invoke self "new" using lnkSize returning lnkobject
           exit method.
       end method "ofValues".
      *>----------------------------------------------------------------
      *> Returns a new instance with the same size and value as
      *> ByteArray instance lnkObject.
      *>----------------------------------------------------------------
       method-id. "withAll".
       linkage section.
       01 lnkobject         usage object reference.
       01 lnknewstring      usage object reference.

       procedure division using lnkobject returning lnknewstring.
           invoke lnkobject "copy" returning lnknewstring.
           exit method.
       end method "withAll".

       end class-object.

       Object.

       object-storage section.
      *>----------------------------------------------------------------
      *> Return True (1) if the value indicated by compareValue and
      *> compareLength is Equal to the value of this instance
      *> else return False (0).
      *>----------------------------------------------------------------
       method-id. "equalbyLengthValue".
       linkage section.
       01 comparevalue  pic x.
       01 lnkbool           pic x comp-5.
           88  lnkistrue  value 1.
           88  lnkisfalse value 0.
       01 comparelength        pic x(4) comp-5.
       01 instanceData          pic x.
       procedure division using comparelength comparevalue
                                            returning lnkbool.
           set address of instancedata to collptr
           if comparevalue(1:compareLength)
                         = instancedata(1:storagerequirements)
               set lnkistrue to true
           else
               set lnkisfalse to true
           end-if
           exit method.
       end method "equalbyLengthValue".
      *>----------------------------------------------------------------
      *> Return 1 if this instance is greater than anArray.
      *> Return -1 if this instance is less than anArray.
      *> Return 0 if this instance is equal to anArray.
      *>----------------------------------------------------------------
       method-id. "compareTo".
       local-storage section.

       01 comparelength       pic x(4) comp-5.
       01 aptr                usage pointer.

       linkage section.
       01 comparevalue        pic x.
       01 anarray              usage object reference.
       01 lnkResult            pic s9(9) comp-5.
           88  lnkGreaterThan value  1.
           88  lnkLessThan    value -1.
           88  lnkEqual       value  0.
       01 instanceData          pic x.
       procedure division using anarray returning lnkResult.
           invoke anarray "sizeInBytes" returning compareLength
           invoke anArray "getAddress" returning aptr
           set address of comparevalue to aptr
           set address of instancedata to collptr
           if instancedata(1:storagerequirements)
                                < comparevalue(1:compareLength)
               set lnkLessThan to true
           else
               if instancedata(1:storagerequirements)
                                > comparevalue(1:compareLength)
                   set lnkGreaterThan to true
               else
                   set lnkEqual to true
               end-if
           end-if
           exit method.
       end method "compareTo".
      *>----------------------------------------------------------------
      *> Return True (1) if the value of this ByteArray is greater
      *> than the first compareLength bytes of compareValue, else return
      *> False (0).
      *>----------------------------------------------------------------
       method-id. "greaterThanByLengthValue".
       linkage section.
       01 comparevalue  pic x.
       01 lnkbool          pic x comp-5.
           88  lnkistrue  value 1.
           88  lnkisfalse value 0.
       01 comparelength        pic x(4) comp-5.
       01 instanceData         pic x.
       procedure division using comparelength comparevalue
                                           returning lnkbool.
           set address of instancedata to collptr
           if  instancedata(1:storagerequirements)
                                > comparevalue(1:compareLength)
               set lnkistrue to true
           else
               set lnkisfalse to true
           end-if
           exit method.
       end method "greaterThanByLengthValue".
      *>----------------------------------------------------------------
      *> Return true (1) if the value of this ByteArray is less
      *> than or equal to the value of anIntrinsic, else return
      *> false (0).
      *>----------------------------------------------------------------
       method-id. "lessThanOrEqual".
       local-storage section.

       01 comparelength    pic x(4) comp-5.
       01 aptr             usage pointer.

       linkage section.
       01 comparevalue   pic x.
       01 anintrinsic      usage object reference.
       01 lnkbool           pic x comp-5.
           88  lnkistrue  value 1.
           88  lnkisfalse value 0.
       01 instanceData          pic x.
       procedure division using anintrinsic returning lnkbool.
           invoke anintrinsic "sizeInBytes" returning compareLength
           invoke anintrinsic "getAddress" returning aptr
           set address of comparevalue to aptr
           set address of instancedata to collptr
           if instancedata(1:storagerequirements)
                                <= comparevalue(1:compareLength)
               set lnkistrue to true
           else
               set lnkisfalse to true
           end-if
           exit method.
       end method "lessThanOrEqual".
      *>----------------------------------------------------------------
      *> Return true (1) if the value of this instance is greater than
      *> or equal to the value of anIntrinsic, else return false (0).
      *>----------------------------------------------------------------
       method-id. "greaterThanOrEqual".
       local-storage section.

       01 comparelength      pic x(4) comp-5.
       01 aptr               usage pointer.

       linkage section.
       01 comparevalue     pic x.
       01 anintrinsic      usage object reference.
       01 lnkbool          pic x comp-5.
           88  lnkistrue  value 1.
           88  lnkisfalse value 0.
       01 instanceData     pic x.

       procedure division using anintrinsic returning lnkbool.
           invoke anintrinsic "sizeInBytes" returning compareLength
           invoke anintrinsic "getAddress" returning aptr
           set address of comparevalue to aptr
           set address of instancedata to collptr
           if instancedata(1:storagerequirements)
                                >= comparevalue(1:compareLength)
               set lnkistrue to true
           else
               set lnkisfalse to true
           end-if
           exit method.
       end method "greaterThanOrEqual".
      *>----------------------------------------------------------------
      *> Return true (1) if the value indicated by compareValue and
      *> compareLength is equal to the value of this instance, ignoring
      *> case, else return false (0). However the comparison ignores the
      *> CompareLength is the number of bytes from
      *> compareValue to compare.
      *>----------------------------------------------------------------
       method-id. "equalsIgnoreCasebyLengthValue".
       local-storage section.

       01 temp                  usage object reference.
       01 copyanobject          usage object reference.

       linkage section.
       01 comparevalue         pic x.
       01 comparelength        pic x(4) comp-5.
       01 lnkbool           pic x comp-5.
           88  lnkistrue  value 1.
           88  lnkisfalse value 0.

       procedure division using comparelength comparevalue
                  returning lnkbool.
           invoke selfClass "withLengthValue" using comparelength
                        compareValue
                                   returning copyanObject
           invoke self "equalsIgnoreCase" using copyanobject
                           returning lnkbool.
           invoke copyanobject "finalize" returning temp
           exit method.
       end method "equalsIgnoreCasebyLengthValue".
      *>----------------------------------------------------------------
      *> Return true (1) if this instance is less than the first
      *> compareLength bytes of compareValue, else return false(0).
      *>----------------------------------------------------------------
       method-id. "lessThanByLengthValue".
       linkage section.
       01 comparevalue      pic x.
       01 lnkbool           pic x comp-5.
           88  lnkistrue  value 1.
           88  lnkisfalse value 0.
       01 comparelength        pic x(4) comp-5.
       01 instanceData         pic x.
       procedure division using comparelength comparevalue
                                         returning lnkbool.
           set address of instancedata to collptr
           if  instancedata(1:storagerequirements)
                                < comparevalue(1:compareLength)
               set lnkistrue to true
           else
               set lnkisfalse to true
           end-if
           exit method.
       end method "lessThanByLengthValue".
      *>----------------------------------------------------------------
      *> Set the value of this instance to lnkString. This method reads
      *> the same number of bytes from lnkstring as the current size of
      *> this ByteArray instance.
      *>----------------------------------------------------------------
       method-id. "setValue".
       01 dbcsString           usage object reference.
       01 numCharacters        pic x(4) comp-5.
       01 lsBool               pic x comp-5.
           88 containsDBCSCharacters           value 1.
           88 doesnotcontainDBCSCharacters     value 0.
       linkage section.
       01 lnkstring     pic x.
       01 instanceData  pic x.
       procedure division using lnkstring.
           if not isSBCSEnv
              invoke DBCSCHaracterArray
                   "isDBCSStringLengthValue"
                       using storageRequirements
                             lnkString
                   returning lsBool
              if containsDBCSCharacters
                  invoke DBCSCHaracterArray "withLengthValue"
                         using storageRequirements
                               lnkString
                       returning dbcsString
                  invoke self "becomes" using dbcsString
                  invoke dbcsString "finalize" returning dbcsString
              else
                  set address of instancedata to collptr
                  move lnkstring(1:storagerequirements) to instancedata
                   (1 : storagerequirements)
              end-if
           else
              set address of instancedata to collptr
              move lnkstring(1:storagerequirements) to instancedata
                   (1 : storagerequirements)
           end-if
           exit method.
       end method "setValue".
      *>----------------------------------------------------------------
      *> Returns the number of bytes required for this instance.
      *>----------------------------------------------------------------
       method-id. "sizeInBytes".
       linkage section.
       01 acollectionsize      pic s9(9) comp-5.

       procedure division returning acollectionsize.
           move numberelements to acollectionsize
           exit method.
       end method "sizeInBytes".


      *>----------------------------------------------------------------
      *> Returns a new ByteArray instance, with the null-terminated
      *> string in lnkString concatenated to the value of this instance.
      *>----------------------------------------------------------------
       method-id. "concatZ".
       local-storage section.
       01 lsString             usage object reference.

       linkage section.
       01 lnkString            pic x.
       01 lnkNewString         usage object reference.

       procedure division using lnkString returning lnkNewString.
           invoke selfclass "withValue" using lnkString
                       returning lsString
           invoke self "concat" using lsString
                       returning lnkNewString
           invoke lsString "finalize" returning lsString
           exit program.
       end method "concatZ".

      *>----------------------------------------------------------------
      *> Returns a new ByteArray instance, with the value of
      *> ByteArray or COBOLPicX instance lnkString concatenated to
      *> the value of this instance.
      *>----------------------------------------------------------------
       method-id. "concat".
       local-storage section.
       01 j                    pic s9(9) comp-5.
       01 i                    pic s9(9) comp-5.
       01 aptr                 usage pointer.
       01 dbcsString           usage object reference.
       01 numCharacters        pic x(4) comp-5.
       01 lsBool               pic x comp-5.
           88 containsDBCSCharacters           value 1.
           88 doesnotcontainDBCSCharacters     value 0.

       linkage section.
       01 lnkworkingstring1     pic x.
       01 lnknewstring      usage object reference.
       01 lnkobject         usage object reference.
       01 lnkworkingstring2     pic x.
       01 instanceData          pic x.
       procedure division using lnkobject returning lnknewstring.
           if not isSBCSEnv
               invoke lnkObject "containsDBCSCharacters"
                   returning lsBool
               if containsDBCSCharacters
                   invoke self "size" returning numCharacters
                   invoke DBCSCHaracterArray "withLengthValue"
                                   using numCharacters
                                         by value collptr
                           returning dbcsString
                   invoke dbcsString "concat" using lnkObject
                                          returning lnknewstring
                   invoke dbcsString "finalize" returning dbcsString
               end-if
           else
               set doesnotcontainDBCSCharacters to true
           end-if
           if doesnotcontainDBCSCharacters
               invoke lnkobject "sizeInBytes" returning i
               move i to j
               add storagerequirements to i
               invoke self "malloc" using i returning aptr
               set address of lnkworkingstring1 to aptr
               set address of instancedata to collptr
               move instancedata(1 : storagerequirements) to
                    lnkworkingstring1(1 : storagerequirements)
               invoke lnkObject "getValue" returning
                    lnkworkingstring1(storagerequirements + 1 : j)
               invoke selfClass "withLengthValue"
                           using i lnkWorkingString1
                       returning lnknewstring
               invoke self "dealloc" using aptr
           end-if
           exit method.
       end method "concat".
      *>----------------------------------------------------------------
      *> Returns the value of this instance into buffer lnkString.
      *>
      *>!Warning
      *>
      *> Check that the buffer lnkString is long enough to hold the
      *> value of this instance before sending the "getValue" message.
      *> If the buffer is not long enough, "getValue" will overwrite
      *> other memory causing either data corruption or a protection
      *> violation.
      *>
      *> You can check the size of this instance by sending it the
      *> message "sizeinbytes".
      *>
      *>#See the method "sizeinbytes".
      *>----------------------------------------------------------------
       method-id. "getValue".
       linkage section.
       01 lnkstring     pic x.
       01 instanceData  pic x.
       procedure division returning lnkstring.
           set address of instancedata to collptr
           move instancedata(1 : storagerequirements)
               to lnkstring(1:storagerequirements)
           exit method.
       end method "getValue".

      *>----------------------------------------------------------------
      *> Returns the address of the value in this instance as a COBOL
      *> pointer.
      *>----------------------------------------------------------------
       method-id. "getAddress".
       linkage section.
       01 lnkPtr        POINTER.
       procedure division returning lnkPtr.
           set lnkPtr to collptr
           exit method.
       end method "getAddress".
      *>----------------------------------------------------------------
      *> Returns the value of this string as a null-terminated string.
      *>
      *>!Warning
      *>
      *> Check that the buffer lnkString is long enough to hold the
      *> value of this instance before sending the "asParameter"
      *> message. If the buffer is not long enough, "asParameter"
      *> overwrites other memory, causing either data corruption
      *> or a protection violation.
      *>
      *> You can check the size of this instance by sending it the
      *> message "sizeinbytes" - but allow one extra byte for the null
      *> byte (x"00) appended to the data by this method.
      *>
      *>#See the method "sizeinbytes".
      *>----------------------------------------------------------------
       method-id. "asParameter".
       linkage section.
       01 lnkstring     pic x.
       01 instanceData  pic x.
       procedure division returning lnkstring.
           set address of instancedata to collptr
           move instancedata(1 : storagerequirements + 1)
               to lnkstring(1:storagerequirements + 1)
           exit method.
       end method "asParameter".

      *>----------------------------------------------------------------
      *> Assumes contents of this instance are numbers and
      *> returns as a PIC S9(9) COMP-5.
      *>----------------------------------------------------------------
       method-id. "asCOMP5".
       local-storage section.
       01 digit        pic 9.
       01 factor       pic x(4) comp-5.
       01 i            pic x(4) comp-5.
       linkage section.
       01 lnkComp5     pic s9(9) comp-5.
       01 instanceData pic x.
       procedure division returning lnkComp5.
           move 1 to factor
           move 0 to lnkComp5
           set address of instancedata to collptr
           perform varying i from storageRequirements by -1 until i = 0
               move instancedata (i :1) to digit
               compute lnkComp5 = lnkComp5 + (digit * factor)
               multiply 10 by factor
           end-perform
           exit method.
       end method "asCOMP5".

      *>----------------------------------------------------------------
      *> Assumes contents of this instance are numbers and
      *> returns the value as a PIC S9(9) COMP-5, in an instance of
      *> COBOLComp5.
      *>
      *>#See the class "comp5".
      *>----------------------------------------------------------------
       method-id. "asCOMP5Object".
       local-storage section.
       01 lsComp5       pic x(4) comp-5.
       linkage section.
       01 lnkComp5     usage object reference.
       procedure division returning lnkComp5.
           invoke self "asComp5" returning lsComp5
           invoke CObolCOmp5 "withValue" using lsComp5
                        returning lnkComp5
           exit method.
       end method "asCOMP5Object".

      *>----------------------------------------------------------------
      *> Display the value of this instance.
      *>----------------------------------------------------------------
       method-id. "display".
       linkage section.
       01 instanceData  pic x.
       procedure division.
           set address of instancedata to collptr
           display instancedata(1 : storagerequirements)
                 with no advancing
           exit method.
       end method "display".

      *>----------------------------------------------------------------
      *> Returns a new string which contains the same characters as this
      *> string, folded to lower-case.
      *>----------------------------------------------------------------
       method-id. "asLowercase".
       local-storage section.
       01 aptr                 usage pointer.
       linkage section.
       01 lnkworkingstring1     pic x.
       01 lnknewstring      usage object reference.

       procedure division returning lnknewstring.
           invoke self "malloc" using storageRequirements
                            returning aptr
           set address of lnkworkingstring1 to aptr
           invoke self "getValue" returning lnkworkingstring1
           call "CBL_TOLOWER" using lnkworkingstring1
                               by value storageRequirements
           invoke selfClass "withlengthvalue" using storageRequirements
                                                 lnkworkingstring1
                                       returning lnknewstring
           invoke self "dealloc" using aptr
           exit method.
       end method "asLowercase".
      *>----------------------------------------------------------------
      *> Returns an OrderedCollection instance of ByteArray
      *> instances. Each new instance represents a separate line of this
      *> instance. Lines are assumed to be delimeted by the linefeed
      *> character (x"0A").
      *>----------------------------------------------------------------
       method-id. "asSeparateLines".
       working-storage section.
       01 linefeed.
           03 intlinefeed      pic x comp-5 value 10.

       local-storage section.

       01 stringlength         pic s9(9) comp-5.
       01 startofline          pic s9(9) comp-5.
       01 i                    pic s9(9) comp-5.
       01 aline                usage object reference.

       linkage section.
       01 lnkcollection        usage object reference.
       01 instanceData         pic x. 
       procedure division returning lnkcollection.
           move 5 to i
           invoke orderedcollection "ofReferences" using i
                                               returning lnkcollection
           move 1 to startofline
           set address of instancedata to collptr
           perform varying i  from 1 by 1 until i > storagerequirements
               if instancedata(i : 1) = linefeed
                   compute stringlength = i - startofline
                   if stringlength = zero
                       continue
                   else
                       invoke ByteArray "withLengthValue"
                          using stringlength
                                instancedata(startofline : stringlength)
                          returning aline
                       invoke lnkcollection "add"
                               using aline
                   end-if
                   move i to startofline
                   add 1 to startofline
               end-if
           end-perform
           if startofline <= storagerequirements
               if instancedata(startofline : 1) not = linefeed
                   compute stringlength = storagerequirements -
                                           startofline + 1
                   if stringlength = zero
                       continue
                   else
                       invoke ByteArray "withLengthValue"
                          using stringlength
                                instancedata(startofline : stringlength)
                          returning aline
                       invoke lnkcollection "add" using aline
                   end-if
               end-if
           end-if
           exit method.
       end method "asSeparateLines".
      *>----------------------------------------------------------------
      *> Return a new instance which contains the same characters as
      *> this instance, folded to upper-case.
      *>----------------------------------------------------------------
       method-id. "asUppercase".
       local-storage section.
       01 aptr                 usage pointer.
       linkage section.
       01 lnkworkingstring1    pic x.
       01 lnknewstring         usage object reference.

       procedure division returning lnknewstring.
           invoke self "malloc" using storageRequirements
                            returning aptr
           set address of lnkworkingstring1 to aptr
           invoke self "getValue" returning lnkworkingstring1
           call "CBL_TOUPPER" using lnkworkingstring1
                               by value storageRequirements
           invoke selfClass "withlengthvalue" using storageRequirements
                                                 lnkworkingstring1
                                       returning lnknewstring
           invoke self "dealloc" using aptr
           exit method.
       end method "asUppercase".

      *>----------------------------------------------------------------
      *> Assumes contents of this instance are hexadecimal numbers and
      *> returns as a PIC 9(9) COMP-5.
      *>----------------------------------------------------------------
       method-id. "asHEX".
       local-storage section.
       01 digit        pic x.
       01 digitByte redefines digit pic x comp-5.
       01 tempByte     pic x comp-5.
       01 factor       pic x(4) comp-5.
       01 i            pic x(4) comp-5.
       linkage section.
       01 lnkComp5     pic 9(9) comp-5.
       01 instanceData pic x.
       procedure division returning lnkComp5.
           move 1 to factor
           move 0 to lnkComp5
           set address of instancedata to collptr
           perform varying i from storageRequirements by -1 until i = 0
               move instancedata (i :1) to digit
               if digit >= "A"
                  subtract 39 from digitByte
               end-if
               move 15 to tempByte
               call "CBL_AND" using tempByte
                                    digitByte
                              by value 1
               compute lnkComp5 = lnkComp5 + (digitByte * factor)
               multiply 16 by factor
           end-perform
           exit method.
       end method "asHEX".

      *>----------------------------------------------------------------
      *> Stores character aValue at position anIndex. Raises an
      *> exception if anindex is outside the bounds of this instance.
      *>---------------------------------------------------------------
       method-id. "atPut".
       local-storage section.
       01 realindex            pic s9(9) comp-5.
       01 charLength           pic x(4) comp-5.
       01 numCharacters        pic x(4) comp-5.
       01 aDBCSString          usage object reference.
       linkage section.
       01 anindex              pic s9(9) comp-5.
       01 avalue               pic x.
       01 instanceData         pic x.
       procedure division using anindex avalue.
           if anindex < 1 or anindex > numberelements
               invoke self "errorinBounds"
           else
               if not isSBCSEnv
                   invoke DBCSCHaracterArray "charLengthInBytes"
                                                       using aValue
                                                   returning charLength
                   if charLength = 1
                       compute realindex = (anindex - 1) + 1
                       set address of instancedata to collptr
                       move avalue to instancedata(realindex : 1)
                   else
                       invoke self "size" returning numCharacters
                       invoke DBCSCHaracterArray "withLengthValue"
                             using numCharacters
                                   by value collptr
                           returning aDBCSString
                       invoke aDBCSString "atPut" using anIndex aValue
                       invoke aDBCSString "becomes" using self
                       invoke aDBCSString "finalize"
                           returning aDBCSString
                   end-if
               else
                   set address of instancedata to collptr
                   move avalue to instancedata(anIndex : 1)
              end-if
           end-if
           exit method.
       end method "atPut".
      *>----------------------------------------------------------------
      *> Returns an instance of COBOLPicx which has the same value as
      *> this instance.
      *>
      *>#See the class "picx".
      *>----------------------------------------------------------------
       method-id. "asPicX".

       local-storage section.

       01 aclass               usage object reference.

       linkage section.
       01 lnknewstring      usage object reference.

       procedure division returning lnknewstring.
           invoke cobolpicx "newClass" using storagerequirements
                                           returning aclass
           invoke aclass "withValue" using by value collptr
                   returning lnknewstring
           exit method.
       end method "asPicX".
      *>----------------------------------------------------------------
      *> Returns a new instance stripped of all leading and trailing
      *> spaces.
      *>----------------------------------------------------------------
       method-id. "trimBlanks".
       local-storage section.

       01 j                    pic s9(9) comp-5.
       01 i                    pic s9(9) comp-5.
       01 b                    pic s9(9) comp-5.

       linkage section.
       01 lnkobject            usage object reference.
       01 instanceData         pic x.
       procedure division returning lnkobject.
           move 1 to b
           set address of instancedata to collptr
           perform varying i from 1 by 1
             until instancedata(i:b) not = space
                   or i > storagerequirements
           end-perform
           perform varying j from storagerequirements by -1
             until instancedata(j:b) not = space
                   or j = 1
           end-perform
           add j to b
           subtract i from b
           if b < 0        *>>> Deals with the case where the
               move 0 to b *>>> whole string is spaces
           end-if
           invoke ByteArray "withLengthValue" using b
                        instancedata(i:b)
                returning lnkobject
           exit method.
       end method "trimBlanks".
      *>----------------------------------------------------------------
      *> Returns true (1) if this instance is less than the
      *> ByteArray anArray else return false (0).
      *>----------------------------------------------------------------
       method-id. "lessThan".
       local-storage section.

       01 comparelength       pic x(4) comp-5.
       01 aptr                usage pointer.

       linkage section.
       01 comparevalue         pic x.
       01 anarray      usage object reference.
       01 lnkbool           pic x comp-5.
           88  lnkistrue  value 1.
           88  lnkisfalse value 0.
       01 instanceData          pic x.
       procedure division using anarray returning lnkbool.
           invoke anarray "sizeInBytes" returning compareLength
           invoke anArray "getAddress" returning aptr
           set address of comparevalue to aptr
           set address of instancedata to collptr
           if instancedata(1:storagerequirements)
                                < comparevalue(1:compareLength)
               set lnkistrue to true
           else
               set lnkisfalse to true
           end-if
           exit method.
       end method "lessThan".
      *>----------------------------------------------------------------
      *> Returns true (1) if this instance is greater than the
      *> ByteArray anArray else return false (0).
      *>----------------------------------------------------------------
       method-id. "greaterThan".
       local-storage section.
       01 comparelength        pic x(4) comp-5.
       01 aptr   usage pointer.

       linkage section.
       01 comparevalue  pic x.
       01 anarray      usage object reference.
       01 lnkbool          pic x comp-5.
           88  lnkistrue  value 1.
           88  lnkisfalse value 0.
       01 instanceData          pic x.
       procedure division using anarray returning  lnkbool.
           invoke anarray "sizeInBytes" returning compareLength
           invoke anArray "getAddress" returning aptr
           set address of comparevalue to aptr
           set address of instancedata to collptr
           if instancedata(1:storagerequirements)
                             > comparevalue(1:compareLength)
               set lnkistrue to true
           else
               set lnkisfalse to true
           end-if
           exit method.
       end method "greaterThan".

      *>----------------------------------------------------------------
      *> Returns true (1) if anotherstring is equal to this instance
      *> else returns false (0).
      *>----------------------------------------------------------------
       method-id. "equal".
       local-storage section.

       01 anotherstringsize        pic x(4) comp-5.
       01 aptr                     usage pointer.
       01 lsBool                   pic x comp-5.
       linkage section.
       01 anotherstring        usage object reference.
       01 arealstring          pic x.
       01 lnkresult             pic x comp-5.
           88 isEqual          value 1.
           88 isNotEqual       value 0.
       01 instanceData         pic x.

       procedure division using anotherstring returning lnkresult.
           invoke anotherString "containsCharacters"
               returning lsBool
           if lsBool = 0
               move zero to lnkResult
           else
               invoke anotherString "sizeInBytes"
                    returning anotherstringsize
               invoke anotherString "getAddress" returning aptr
               set address of arealString to aptr
               set address of instancedata to collptr
               if arealstring(1:anotherstringsize) =
                    instancedata(1 : storagerequirements)
                   set isEqual to true
               else
                   set isNotEqual to true
               end-if
           end-if
           exit method.
       end method "equal".

      *>----------------------------------------------------------------
      *> Returns true (1) if anObject is equal to this instance,
      *> ignoring case of characters, else returns false (0).
      *>----------------------------------------------------------------
       method-id. "equalsIgnoreCase".
       local-storage section.

       01 temp                  usage object reference.
       01 copyself                 usage object reference.
       01 copyanobject             usage object reference.

       linkage section.
       01 anobject         usage object reference.
       01 lnkbool          pic x comp-5.
           88  lnkistrue  value 1.
           88  lnkisfalse value 0.

       procedure division using anobject
                       returning lnkbool.
           invoke self "asUpperCase" returning copyself
           invoke anobject "asUpperCase" returning copyanobject
           invoke copyself "equal" using copyanobject
                                    returning lnkbool
           invoke copyself "finalize" returning temp
           invoke copyanobject "finalize" returning temp
           exit method.
       end method "equalsIgnoreCase".
      *>----------------------------------------------------------------
      *> Move the contents of lnkString to this instance. If lnkString
      *> is longer than this string, the contents are truncated. If it
      *> is shorter, then the remaining space in this instance is
      *> space filled.
      *>----------------------------------------------------------------

       method-id. "moveValue".
       local-storage section.
       01 i        pic x(4) comp-5.
       01 aptr     usage pointer.
       01 dbcsString           usage object reference.
       01 numCharacters        pic x(4) comp-5.
       01 lsBool               pic x comp-5.
           88 containsDBCSCharacters           value 1.
           88 doesnotcontainDBCSCharacters     value 0.
       linkage section.
       01 lnkString         usage object reference.
       01 lnkRealString     pic x.
       01 instanceData      pic x.
       procedure division using lnkString.
           if not isSBCSEnv
               invoke lnkString "containsDBCSCharacters"
                           returning lsBool
               if containsDBCSCharacters
                   invoke self "size" returning numCharacters
                   invoke DBCSCHaracterArray "withLengthValue"
                       using numCharacters
                             by value collptr
                   returning dbcsString
                   invoke self "becomes" using dbcsString
                   invoke dbcsString "finalize" returning dbcsString
                   invoke self "moveValue" using lnkString
               end-if
           else
               set doesnotcontainDBCSCharacters to true
           end-if
           if doesnotcontainDBCSCharacters
               invoke lnkString "sizeInBytes" returning i
               invoke self "malloc" using i returning aptr
               set address of lnkRealString to aptr
               invoke lnkString "getValue" returning lnkRealString
               set address of instancedata to collptr
               move lnkRealString(1:i) to
                        instanceData(1:storageRequirements)
               invoke self "dealloc" using aptr
           end-if
           exit method.
       end method "moveValue".
      *>----------------------------------------------------------------
      *> Reset the contents of this instance so it has the same contents
      *> as lnkString. This method also resets the size of this
      *> instance.
      *>----------------------------------------------------------------
       method-id. "resetValue".
       local-storage section.
       01 dbcsString           usage object reference.

       linkage section.
       01 lnkString         usage object reference.
       procedure division using lnkString.
           invoke lnkString "copy" returning dbcsString
           invoke self "becomes" using dbcsString
           invoke dbcsString "finalize" returning dbcsString
           exit method.
       end method "resetValue".
      *>----------------------------------------------------------------
      *> Returns a string containing a description of this string.
      *>----------------------------------------------------------------
       method-id. "description".
       local-storage section.
       01 lsSize       pic x(4) comp-5.
       01 lsPtr        usage pointer.
       linkage section.
       01 lnkString    usage object reference.
       01 lnkRealValue pic x.
       procedure division returning lnkString.
           invoke self "sizeInBytes" returning lsSize
           add 2 to lsSize
           invoke self "malloc" using lsSize returning lsPtr
           set address of lnkRealValue to lsPtr
           move """" to lnkRealValue(1:1)
           invoke self "getvalue" returning lnkRealValue(2 : lsSize)
           move """" to lnkRealValue(lsSize : 1)
           invoke selfClass "withLengthValue" using lsSize lnkRealValue
               returning lnkString
           invoke self "dealloc" using lsPtr
           exit method.
       end method "description".
      *>----------------------------------------------------------------
      *> Returns the hash value of this instance, calculated from its
      *> contents.
      *>----------------------------------------------------------------
       method-id. "hash".
       local-storage section.

       01 achar                    pic x.
       01 numchar redefines achar  pic x comp-5.
       01 y                        pic x(4) comp-5.
       01 j                        pic x(4) comp-5.
       01 i                        pic x(4) comp-5.

       linkage section.
       01 ahashvalue       pic s9(9) comp-5.
       01 instanceData     pic x.
       procedure division returning ahashvalue.
           move zero to ahashvalue
           move 1 to j
           set address of instancedata to collptr
           perform varying i from storagerequirements by -1
              until i = zero or
                    instancedata(i:j) not  = space
           end-perform
           move i to y
           perform varying i from y by -1 until i = zero
               move instancedata(i:j) to achar
               compute ahashvalue = ahashvalue * 47 + numchar
           end-perform
           if ahashvalue < 0
               compute ahashvalue = - ahashvalue
           end-if
           exit method.
       end method "hash".
      *>----------------------------------------------------------------
      *> Returns true (1) if this collection and aCollection are
      *> compatible.
      *> Any instance method that takes a collection as a parameter must
      *> be compatible with the receiver of the message. An exception
      *> will be raised if the collections are incompatible.
      *>----------------------------------------------------------------
       method-id. "isCompatible".
       linkage section.
       01 lnkbool                pic x comp-5.
           88  lnkistrue          value 1.
           88  lnkisfalse         value 0.
       01 acollection          usage object reference.

       procedure division using acollection
                        returning lnkbool.
           invoke self "containsCharacters" returning lnkBool
           exit method.
       end method "isCompatible".

      *>----------------------------------------------------------------
      *> Returns true if this type of collection contains characters.
      *>----------------------------------------------------------------
       method-id. "containsCharacters".
       linkage section.
       01 lnkbool                pic x comp-5.
           88  lnkistrue          value 1.
           88  lnkisfalse         value 0.
       01 acollection          usage object reference.

       procedure division returning lnkbool.
           set lnkIsTrue to true
           exit method.
       end method "ContainsCharacters".
      *>----------------------------------------------------------------
      *> Causes this instance to become larger or smaller
      *> according to the requiredSize parameter.
      *>----------------------------------------------------------------
       method-id. "growTo".
       local-storage section.
       01 exceptionnumber  pic s9(9) comp-5.
       01 bool                 pic x comp-5.
          88 istrue           value 1.
          88 isfalse          value 0.

       01 newSize              pic s9(9) comp-5.


       linkage section.
       01 defaultreturn        usage object reference.
       01 requiredSize         pic x(4) comp-5.


       procedure division using requiredSize returning defaultreturn.
           move requiredsize to newsize 
           add 1 to newsize
           invoke self "basicGrow" using newsize
                                    returning bool
           if isfalse
               move 32 to exceptionnumber
               invoke self "raiseException" using exceptionnumber
                                       returning defaultreturn
           else
               move requiredsize to storagerequirements
                                    numberElements
               set defaultreturn to self
           end-if

           exit method.
       end method "growTo".

      *>----------------------------------------------------------------
      *> Returns the first lnksize bytes of this instance into buffer
      *> lnkString. You must ensure that lnkString is at least lnkSize
      *> bytes long, or other memory is overwritten, causing either
      *> data corruption or a protection violation.
      *>----------------------------------------------------------------
       method-id. "getValueWithSize".
       linkage section.
       01 lnksize          pic 9(9) comp-5.
       01 lnkstring        pic x.
       01 instanceData     pic x.
        
       procedure division using lnksize returning lnkstring.
           set address of instancedata to collptr
           if storagerequirements < lnksize
             move instancedata(1 : storagerequirements)
               to lnkstring(1 : lnksize)
           else
             move instancedata(1 : lnksize)
               to lnkstring(1 : lnksize)
           end-if
           exit method.
       end method "getValueWithSize".
      *>----------------------------------------------------------------
      *> Returns the lnksize bytes of this instance into buffer
      *> lnkString, starting from position lnkOffset. You must ensure
      *> that lnkString is at least lnkSize
      *> bytes long, or other memory is overwritten, causing either
      *> data corruption or a protection violation.
      *>----------------------------------------------------------------
       method-id. "getValueWithSizeAtPosition".
       linkage section.
       01 lnksize          pic 9(9) comp-5.
       01 lnkoffset        pic 9(9) comp-5.
       01 lnkstring        pic x.
       01 instanceData     pic x.
       procedure division using lnkoffset lnksize returning lnkstring.
           set address of instancedata to collptr
           if storagerequirements < lnksize + lnkoffset - 1
             move instancedata(lnkoffset : storagerequirements)
               to lnkstring(1 : lnksize)
           else
             move instancedata(lnkoffset : lnksize)
               to lnkstring(1 : lnksize)
           end-if
           exit method.
       end method "getValueWithSizeAtPosition".

      *>----------------------------------------------------------------
      *> Delimits the character array by white space and places each
      *> delimited string element into an Array of references.
      *> White space in this method, is defined as being any character
      *> with an ascii value that is less than or equal to 32 (space).
      *>----------------------------------------------------------------
      *>----------------------------------------------------------------
      *> Returns an Array instance of ByteArray
      *> instances. The new ByteArray instances represent this
      *> instance divided into substrings by white space.
      *> This metod defines white space as any character with ASCII
      *> value <= 32 (space character).
      *>----------------------------------------------------------------
       method-id. "asArrayOfSubStrings".

       local-storage section.
       01 lsByteSize                   pic s9(9) comp-5.
       01 lsCharSize                   pic s9(9) comp-5.
       01 lsPtr                        usage pointer.
       01 lsCollectionSize             pic s9(9) comp-5.
       01 lsOrderedCollectionObject    usage object reference.
       01 lsSearchStatus               pic x comp-5.
          88 lsSearchForNonNoiseChar               value 0.
          88 lsSearchForNoiseChar                  value 1.
       01 lsWordStart                  pic s9(9) comp-5.
       01 lsWordCharLength             pic s9(9) comp-5.
       01 lsCharIndex                  pic s9(9) comp-5.
       01 lsWordObject                 usage object reference.

       linkage section.
       01 lnkArrayObject               usage object reference.
       01 lnkRealString                pic x.

       procedure division returning lnkArrayObject.
           invoke self "sizeInBytes"
                       returning lsByteSize
           invoke self "malloc"
                           using lsByteSize
                       returning lsPtr
           set address of lnkRealString to lsPtr
           invoke self "getValue"
                       returning lnkRealString
           move 10 to lsCollectionSize
           invoke OrderedCollection "ofReferences"
                           using lsCollectionSize
                       returning lsOrderedCollectionObject
           invoke self "size"
                       returning lsCharSize
           set lsSearchForNonNoiseChar to true
           move zero to lsWordCharLength
           perform varying lsCharIndex from 1 by 1
             until lsCharIndex > lsCharSize
               if lsSearchForNonNoiseChar
                   if lnkRealString(lsCharIndex:1) <= space
                       continue
                   else
                       move lsCharIndex to lsWordStart
                       add 1 to lsWordCharLength
                       set lsSearchForNoiseChar to true
                   end-if
               else
                   if lnkRealString(lsCharIndex:1) <= space
                       set lsSearchForNonNoiseChar to true
                       invoke ByteArray "withLengthValue"
                                       using lsWordCharLength
                                             lnkRealString(lsWordStart:
                                               lsWordCharLength)
                                   returning lsWordObject
                       invoke lsOrderedCollectionObject "add"
                                       using lsWordObject
                       move zero to lsWordCharLength
                   else
                       add 1 to lsWordCharLength
                   end-if
               end-if
           end-perform
           if lsWordCharLength = zero
               continue
           else
               invoke ByteArray "withLengthValue"
                               using lsWordCharLength
                                     lnkRealString(lsWordStart:
                                       lsWordCharLength)
                           returning lsWordObject
               invoke lsOrderedCollectionObject "add"
                               using lsWordObject
           end-if
           invoke lsOrderedCollectionObject "asArray"
                       returning lnkArrayObject
           invoke lsOrderedCollectionObject "finalize"
                       returning lsOrderedCollectionObject
           invoke self "dealloc"
                           using lsPtr
           exit method.
       end method "asArrayOfSubStrings".

      *>----------------------------------------------------------------
      *> Search through this instance starting at position
      *> lnkStartCharIndexPosition, looking for the first occurrence
      *> of the contents of lnkSearchByteArray.
      *> If the search is sucessful, then the index of
      *> lnkSearchByteArray within this instance is
      *> returned in lnkCharIndexPosition. If unsuccessful, zero is
      *> returned.
      *>----------------------------------------------------------------
       method-id. "indexOfSubCollection".

       local-storage section.
       01 aPtr                         usage pointer.
       01 exceptionNumber              pic s9(9) comp-5.
       01 searchCharSize               pic s9(9) comp-5.
       01 searchByteSize               pic s9(9) comp-5.
       01 indexOffset                  pic x(4) comp-5.
       01 charIndexPosition            pic x(4) comp-5.
       01 aBool                        pic x comp-5.
          88 isFalse                                   value 0.
          88 isTrue                                    value 1.

       linkage section.
       01 lnkSearchByteArray      usage object reference.
       01 lnkStartCharIndexPosition    pic s9(9) comp-5.
       01 lnkCharIndexPosition         pic s9(9) comp-5.

       01 lnkSearchString              pic x.
       01 instanceData                 pic x.

       procedure division using lnkSearchByteArray
                                lnkStartCharIndexPosition
                      returning lnkCharIndexPosition.

           invoke self "isCompatible"
                           using lnkSearchByteArray
                       returning aBool
           if isTrue
               move zero to lnkCharIndexPosition
               invoke lnkSearchByteArray "size"
                           returning searchCharSize
               if searchCharSize = zero
                   move 11 to exceptionnumber
                   invoke self "raiseException"
                                   using exceptionNumber
                               returning lnkCharIndexPosition
               else
                   invoke lnkSearchByteArray "sizeInBytes"
                               returning searchByteSize
                   invoke self "malloc"
                                   using searchByteSize
                               returning aPtr
                   set address of lnkSearchString to aPtr
                   invoke lnkSearchByteArray "getValue"
                               returning lnkSearchString
                   move lnkStartCharIndexPosition to indexOffset
                   set address of instancedata to collptr
                   perform varying charIndexPosition
                     from lnkStartCharIndexPosition by 1
                     until charIndexPosition > numberElements or
                           charIndexPosition + searchCharSize - 1
                           > numberElements or
                           lnkCharIndexPosition not = zero
                       if instanceData(indexOffset:searchByteSize) =
                          lnkSearchString(1:searchByteSize)
                           move charIndexPosition to
                                lnkCharIndexPosition
                       else
                           add 1 to indexOffset
                       end-if
                   end-perform
                   invoke self "dealloc" using aPtr
               end-if
           else
               invoke self "compatibilityError"
                           returning lnkCharIndexPosition
           end-if
           exit method.
       end method "indexOfSubCollection".
      *>----------------------------------------------------------------
      *> Return a new ByteArray instance containing the same
      *> characters as this instance between indices lnkStartIndex and
      *> lnkEndIndex.
      *>----------------------------------------------------------------
       method-id. "copyFromTo".
       local-storage section.
       01 lsCharSize           pic s9(9) comp-5.
       01 lsExceptionNumber    pic s9(9) comp-5.

       linkage section.
       01 lnkStartIndex        pic s9(9) comp-5.
       01 lnkEndIndex          pic s9(9) comp-5.
       01 lnkNewByteArray usage object reference.
       01 instanceData         pic x.
       

       procedure division using lnkStartIndex
                                lnkEndIndex
                      returning lnkNewByteArray.

           subtract lnkStartIndex from lnkEndIndex
                                giving lsCharSize
           add 1 to lsCharSize
           if lsCharSize < 1
               invoke self "errorInBounds"
                           returning lnkNewByteArray
           else
               set address of instancedata to collptr
               invoke ByteArray "withLengthValue"
                            using lsCharSize
                                  instanceData(lnkStartIndex:lsCharSize)
                        returning lnkNewByteArray
           end-if
           exit method.
       end method "copyFromTo".

      *>----------------------------------------------------------------
      *> Returns the value of this string as a null-terminated string.
      *>
      *>!Warning
      *>
      *> Check that the buffer lnkString is long enough to hold the
      *> value of this instance before sending the "getValueZ"
      *> message. If the buffer is not long enough, "getValueZ"
      *> overwrites other memory, causing either data corruption
      *> or a protection violation.
      *>
      *> You can check the size of this instance by sending it the
      *> message "sizeinbytes" - but allow one extra byte for the null
      *> byte (x"00) appended to the data by this method.
      *>
      *>#See the method "sizeinbytes".
      *>----------------------------------------------------------------
       method-id. "getValueZ".
       linkage section.
       01 lnkstring     pic x.
       01 instanceData  pic x.
       procedure division returning lnkstring.
           set address of instancedata to collptr
           move instancedata(1:storagerequirements)
               to lnkstring(1:storagerequirements)
           move x"00" to lnkString(storageRequirements + 1: 1)
           exit method.
       end method "getValueZ".

      *>----------------------------------------------------------------
      *> Returns a new ByteArray instance, converting this instance
      *> from an external filename to an internal one. This means
      *> stripping any enclosing quotes from the value.
      *>----------------------------------------------------------------
       method-id. "asInternalFilename".
       local-storage section.
       01 filenameOut           pic x(256).
       01 lsPtr                 pointer.
       01 lsNumBytes            pic x(4) comp-5.
       01 lsLength              pic x(4) comp-5.
       01 func                  pic x comp-x.
       linkage section.
       01 lnkFilename           usage object reference.
       procedure division returning lnkFilename.
      $if visoc10 defined
           invoke self "copy" returning lnkFilename
      $else
           invoke self "sizeInBytes" returning lsNumBytes
           move 2 to func
           invoke self "getAddress" returning lsPtr
           move low-values to filenameOut
           call "CBL_FILENAME_CONVERT" using func
                                        by value lsPtr
                                        by reference fileNameOut
                                        by value lsNumBytes
                                        by value 255
                returning lsLength
           invoke ByteArray "withValue" using fileNameOut                                                  
                                returning lnkFileName
      $end
           exit method.
       end method "asInternalFilename".

      *>----------------------------------------------------------------
      *> Return a copy of this object as it is already a character
      *> array.
      *>----------------------------------------------------------------
       method-id. "asByteArray".
       local-storage section.
       linkage section.
       01 lnkCopy                usage object reference.

       procedure division returning lnkCopy.
            invoke self "copy" returning lnkCopy
            exit program.
       end method "asByteArray".

       end object.
       end class bytearray.
