Topic
6 replies Latest Post - ‏2013-11-25T09:53:31Z by pommCannelle
Gedinfo
Gedinfo
78 Posts
ACCEPTED ANSWER

Pinned topic How do I replace text in a buffer?

‏2013-11-21T17:08:10Z |

I have a file, which I read into a buffer. 

I wish to be able to replace a certain string in this buffer, which may appear on multiple lines.

I have a  function which replaces a string within a string, and was wondering if there exists a similar function for the buffer 

 

For example: the file contains the following:

 

2955 PASS
2956 REWORK: last status was:PASS
 

I wish to be able to replace 

2956 REWORK: last status was:PASS

and end up with 

2956 PASS (Replacing REWORK: last status was: with a blank

 

The file consists of many REWORK lines, and exceeds 255 characters.

When I attempt to use stringOf(buffer), I get a 254 character restriction.

My original thought was to put the file into a buffer, turn it into a string, modify the string, then convert the string back into the buffer.

Any suggestions?

 

Thank you

  • GregM_dxler
    GregM_dxler
    120 Posts
    ACCEPTED ANSWER

    Re: How do I replace text in a buffer?

    ‏2013-11-21T19:44:27Z  in response to Gedinfo

    Hi,

    There is no replace string in a buffer that I know of, but you could make one fairly easy using the contains or search commands and then extract everything except the string from the buffer (buffer = buffer[0:start of string -1] buffer[start of string + string length: ] ).  Probably would need to have this in a loop to catch all instances of the string.  Make sure you keep track of the index, as it will change when you extract the first string match.

    Hope this helps,

    Greg

  • This reply was deleted by mcnairk 2013-11-22T13:37:04Z. Reason for deletion: Posted in error
  • pommCannelle
    pommCannelle
    31 Posts
    ACCEPTED ANSWER

    Re: How do I replace text in a buffer?

    ‏2013-11-22T14:28:28Z  in response to Gedinfo

    You can try this function ...

    Buffer replace ( Buffer b, string toReplace, string newVal ){
        Buffer rez = create
        int pivot = 0
        int iposition = contains ( b, toReplace, pivot )
        while ( iposition != -1 ) {
            rez += b[pivot:iposition-1]
            rez += newVal
            pivot = iposition + length toReplace
            iposition = contains ( b, toReplace, pivot )
        }
        rez += b[pivot:]
        return rez
    }

    Buffer b = create
    b += "text to replace is here"

    Buffer newBuff = replace( b, "is", "was" )
    print tempStringOf newBuff

    delete b
    delete newBuff

    Of course you have to add all the verifications needed ( to check if arguments are not null for example ... ). But i hope this code helps you to start ;)
    Post your code if you are in trouble !
     

    • Mathias Mamsch
      Mathias Mamsch
      1762 Posts
      ACCEPTED ANSWER

      Re: How do I replace text in a buffer?

      ‏2013-11-23T20:59:48Z  in response to pommCannelle

      Be aware of a memory leak in that code...

      rez += b[pivot:iposition-1]

      will leak a Buffer object every time you call it. The reason is, that doors defines the following perms:

      Buffer ::+= (Buffer, Buffer)
      Buffer ::+= (Buffer, string)
      string ::[] (Buffer, Range_)
      Buffer ::[] (Buffer, Range_)

      which allow you to do:

      string s = buf[0:10]  // calls the string ::[] (Buffer, Range_) substring variant
      Buffer b = buf[0:10]  // calls the Buffer ::[] (Buffer, Range_) substring variant

      Unfortunately if you do:

      buf1 +=  buf2[0:10]

      then this could mean both substring variants and DXL will choose the Buffer variant and therefore a temporary Buffer will be created for the substring, its contents appended to buf1, and then the temporary buffer will create a memory leak. Therefore you should always use combine(...) for appending a buffer to another one.

      You can see the leakage, by executing the following code:

      int *::+(int *ptr1, int ofs) { int *ptr2 = ptr1; ptr2+=ofs; return ptr2 }
      int *::@(int *ptr, int ofs) { int ad = *(ptr + ofs); int *ptr2 = addr_ ad; return ptr2 }
      int *getCurrentDXLContextPtr () {
         DB x = create "";
         int *ptr = addr_ x; int *result = ptr @ 48;
         destroy x; return result;
      }   

      int *getMemoryBlockNodes (int *cc) { return cc @ 0x74 }
      int *nextNode (int *memNode) { return memNode @ 8 }   
      int countAllocatedObjects() {
         int *memBlocks = getMemoryBlockNodes getCurrentDXLContextPtr()
         int count = 0
         while (!null memBlocks) { memBlocks = nextNode memBlocks; count++ }
         return count
      }   

      // comment me in to see the object counts increase;  
      print "Allocated Objects Before doing stuff: " countAllocatedObjects() "\n"
      Buffer buf1 = create(), buf2 = create();
      buf1 = "HelloLeak!";  
      print "After allocating buf1 & buf2: " countAllocatedObjects() "\n"

      // now leak three objects, with the statment of that code
      buf2 += buf1[5:8];
      buf2 += buf1[5:8];
      buf2 += buf1[5:8];

      print "Allocated objects after Appening three times: " countAllocatedObjects() "\n"
      print buf2 "!\n"

      Sorry for going off topic, but that is a very common mistake and the code should not be copied like this. Regards, Mathias

      Updated on 2013-11-23T21:02:57Z at 2013-11-23T21:02:57Z by Mathias Mamsch
  • Gedinfo
    Gedinfo
    78 Posts
    ACCEPTED ANSWER

    Re: How do I replace text in a buffer?

    ‏2013-11-22T17:54:39Z  in response to Gedinfo

    Thank you very much for your suggestions.

    • pommCannelle
      pommCannelle
      31 Posts
      ACCEPTED ANSWER

      Re: How do I replace text in a buffer?

      ‏2013-11-25T09:53:31Z  in response to Gedinfo

      ( hereafter the corrected version, according the Mathias entry ;)

      void combine ( Buffer b, string s ) {
          Buffer tmp = create
          tmp = s
          combine ( b, tmp, 0 )
          delete tmp
      }

      Buffer replace ( Buffer b, string toReplace, string newVal ){
          Buffer rez = create
          int pivot = 0
          int iposition = contains ( b, toReplace, pivot )
          while ( iposition != -1 ) {
              combine ( rez, b, pivot, iposition-1 )
              combine ( rez, newVal )
              pivot = iposition + length toReplace
              iposition = contains ( b, toReplace, pivot )
          }
          combine ( rez, b, pivot )
          return rez
      }

      Buffer b = create
      b = "text to replace is here"
      Buffer newBuff = replace( b, "is", "was" )
      print tempStringOf newBuff

      delete b
      delete newBuff

      C u soon ! ;)

      )