Re: No way of looking for a regrexp match starting from a particular point in a string?



On 6/4/07, Robert Klemme <shortcutter@xxxxxxxxxxxxxx> wrote:
On 04.06.2007 14:06, Robert Dober wrote:
> On 6/4/07, Robert Klemme <shortcutter@xxxxxxxxxxxxxx> wrote:
>> On 04.06.2007 13:28, Robert Dober wrote:
>
>> Robert, actually string[n..-1] is cheaper than you might assume: I
>> believe the new string shares the char buffer with the old string, so
>> you basically just get a new String object with a different offset - the
>> large bit (the char data) is not copied.
> I am afraid that this is not true anymore when the slice is passed as
> a formal parameter, the data has to be copied :(
>
> irb(main):011:0> def change(x)
> irb(main):012:1> x << "changed"
> irb(main):013:1> end
> => nil
> irb(main):014:0> a="abcdef"
> => "abcdef"
> irb(main):015:0> change(a[1..2])
> => "bcchanged"
> irb(main):016:0> a
> => "abcdef"

Copying in this case is not caused by using the string as a parameter
but by appending to it.

I thought this thread was about /scanning/ which is a read only
operation. Did I miss something?
No you did not, theoretically it might work like this:

def change( x )
x << changed # copy on write
end

a="some string"
b=a[1..3] # shallow copy
b << "changed" # copy on write
a << "changed" # no copy of course

but do you think it does? Note that the object must have state to know
when and how to copy the underlying data, I am about to read string.c
but it is quite complicated and I got some work to do :(.

Cheers
Robert


Kind regards

robert




--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

.



Relevant Pages

  • Re: healp reading / writing binary strings.
    ... Robert Klemme wrote: ... > And how can I write the same type of string from say an ... to the ascii table as well? ... for hex, does the hex value I supply correspond to the ascii table as ...
    (comp.lang.ruby)
  • Re: String.valueOf() Memory Leak inside of thread.
    ... Robert Klemme wrote: ... Each iteration of that loop instantiates an anonymous thread ... If I change the line to read "String ... Regarding number 1, using the NetBeans profiler, I'm seeing that each ...
    (comp.lang.java.programmer)
  • Re: Logical Operations on Strings
    ... Robert Klemme wrote: ... Actually I designed it that way so that I could change only part of the original string without having to create a mask of 0xFF for the remaining characters. ... "hex number" in Ruby, a number is a number, hex is just one ...
    (comp.lang.ruby)
  • Re: Date & time validation
    ... returned as true because the date format is correct, the string above ... Robert Klemme wrote: ... Just parse it into a Time instance: ...
    (comp.lang.ruby)
  • Re: Indexing by multiple keys
    ... Robert Klemme wrote: ... model an SSN as a String than an int. ... I myself would model an SSN as a string. ...
    (comp.lang.java.programmer)

Loading