Recent

Author Topic: How? A Completely INDEPENDENT Copy of UnicodeString  (Read 902 times)

del

  • Full Member
  • ***
  • Posts: 113
How? A Completely INDEPENDENT Copy of UnicodeString
« on: October 19, 2019, 12:22:01 pm »
I'm in Python hell right now and I don't know how to get out. I need to make a copy - we'll call it B - of A that never ever ever ever gets affected by anything that happens to A, from now until Kingdom Come. Here's some pseudo code:

Code: Pascal  [Select]
  1. var A, B: UnicodeString;
  2. begin
  3.   A := 'Hello';
  4.   B := A;
  5.   A := 'Pony';
  6.   A := 'Gravy';
  7.  

I don't want B to be attached at the hip to A. I don't want B to be 'Pony', or 'Gravy'. That's A's job. I want B to be content with just being 'Hello' until I come up with some other plans. How do I do this? I've tried element-by-element copying. I've even thought about making an intermediate copy into an array of integers. There's gotta be an easy way to do this. Thanks in advance.

Thaddy

  • Hero Member
  • *****
  • Posts: 9183
Re: How? A Completely INDEPENDENT Copy of UnicodeString
« Reply #1 on: October 19, 2019, 12:29:47 pm »
UniqueString() will give you a completely independent copy.
https://www.freepascal.org/docs-html/rtl/system/uniquestring.html
Code: Pascal  [Select]
  1. {$mode objfpc}{$H+}
  2. var A, B: UnicodeString;
  3. begin
  4.   A := 'Hello';
  5.   B := A;
  6.   UniqueString(B);
  7.   A := 'Pony';
  8.   A := 'Gravy';
  9.   writeln(A); // prints Gravy
  10.   writeln(B); // prints Hello
  11. end.
« Last Edit: October 19, 2019, 12:42:49 pm by Thaddy »
also related to equus asinus.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7503
Re: How? A Completely INDEPENDENT Copy of UnicodeString
« Reply #2 on: October 19, 2019, 01:05:46 pm »
Note that on string level B will never be Pony or Gravy.

Only if you access the contents using pointers or other lowlevel stuff  it matters.

If you use string level or character level access (with s[_x]), you don't need to worry about this, the copy on write mechanism will fix this.

That is also why Thaddy's example will print exactly the same thing without the uniquestring(), he doesn't do any low level access in the example.

p.s. added underscore in s[_x] to avoid forum software or browser making assumptions about tags.
« Last Edit: October 19, 2019, 02:21:20 pm by marcov »

del

  • Full Member
  • ***
  • Posts: 113
Re: How? A Completely INDEPENDENT Copy of UnicodeString
« Reply #3 on: October 19, 2019, 01:07:28 pm »
UniqueString() will give you a completely independent copy.
https://www.freepascal.org/docs-html/rtl/system/uniquestring.html
Code: Pascal  [Select]
  1. {$mode objfpc}{$H+}
  2. var A, B: UnicodeString;
  3. begin
  4.   A := 'Hello';
  5.   B := A;
  6.   UniqueString(B);
  7.   A := 'Pony';
  8.   A := 'Gravy';
  9.   writeln(A); // prints Gravy
  10.   writeln(B); // prints Hello
  11. end.

Wow! - Thanks, Thaddy - that did the trick. It was giving me fits as I was loading objects containing UnicodeString onto TVector. Your suggestion seems to have swept aside all obstacles - thanks again !!
 :)

Thaddy

  • Hero Member
  • *****
  • Posts: 9183
Re: How? A Completely INDEPENDENT Copy of UnicodeString
« Reply #4 on: October 19, 2019, 01:27:29 pm »
Yes. The case Marco describes does not fit your requirements. (But is a good thing to keep in mind!)
also related to equus asinus.

del

  • Full Member
  • ***
  • Posts: 113
Re: How? A Completely INDEPENDENT Copy of UnicodeString
« Reply #5 on: October 19, 2019, 08:28:19 pm »
Well, now that I've gotten some sleep I'm gonna take my code back to what it was when I first encountered this problem - a two dimensional expanding TVector of TVector. I read somewhere that push_back in std::vector called the copy constructor. So I made what I thought was a "unique" copy constructor. But at some point I became completely confused and some of what I reported may not have been accurate, i.e, the element-by-element copying might have worked after all. At any rate, I'm unwinding all my changes and starting over with a known solution. This whole thing reminded me of this:

https://stackoverflow.com/questions/19951816/python-changes-to-my-copy-variable-affect-the-original-variable

And this was me (8 - in the comments):

Quote
8
I just spend the past 3 hours trying to debug my program... I thought I was going mad. This was the most infuriating bug I have EVER ran into. Wow... – 43.52.4D. Mar 17 '16 at 7:05
12
@43.52.4D. Only it's not a bug :-) – yuvi Mar 17 '16 at 7:13

Not really me but this is where my mind was.
 :)

dogriz

  • Full Member
  • ***
  • Posts: 105
    • Tech blog - Delphi, Lazarus, Firebird, Windows, Linux, Android...
Re: How? A Completely INDEPENDENT Copy of UnicodeString
« Reply #6 on: October 21, 2019, 07:50:20 am »
What about
Code: [Select]
  B := copy(A, 1, Length(A));
?
FPC 3.0.4
Lazarus 2.0.2
Debian x86_64, arm

PascalDragon

  • Hero Member
  • *****
  • Posts: 673
  • Compiler Developer
Re: How? A Completely INDEPENDENT Copy of UnicodeString
« Reply #7 on: October 21, 2019, 09:02:45 am »
Well, now that I've gotten some sleep I'm gonna take my code back to what it was when I first encountered this problem - a two dimensional expanding TVector of TVector. I read somewhere that push_back in std::vector called the copy constructor. So I made what I thought was a "unique" copy constructor. But at some point I became completely confused and some of what I reported may not have been accurate, i.e, the element-by-element copying might have worked after all. At any rate, I'm unwinding all my changes and starting over with a known solution. This whole thing reminded me of this:

https://stackoverflow.com/questions/19951816/python-changes-to-my-copy-variable-affect-the-original-variable

And this was me (8 - in the comments):

Quote
8
I just spend the past 3 hours trying to debug my program... I thought I was going mad. This was the most infuriating bug I have EVER ran into. Wow... – 43.52.4D. Mar 17 '16 at 7:05
12
@43.52.4D. Only it's not a bug :-) – yuvi Mar 17 '16 at 7:13

Not really me but this is where my mind was.
 :)
Why are you talking about C++ related rules in a Pascal related thread? Pascal does not have copy constructors or such things, so even if we have a TVector that's inspired by the STL it does not behave the same way.

What about
Code: [Select]
  B := copy(A, 1, Length(A));
?
The assignment with the optional call to UniqueString is the recommended solution. The call to Copy is a bit more complex as the mere assignment even if both allocate a full, new string.

del

  • Full Member
  • ***
  • Posts: 113
Re: How? A Completely INDEPENDENT Copy of UnicodeString
« Reply #8 on: October 21, 2019, 08:19:11 pm »
OK - good luck with your dead language. I'm outta here.

440bx

  • Hero Member
  • *****
  • Posts: 1199
Re: How? A Completely INDEPENDENT Copy of UnicodeString
« Reply #9 on: October 21, 2019, 08:54:29 pm »
OK - good luck with your dead language. I'm outta here.
It's not quite dead, at least not yet and, something about the language must have brought you here.

No matter what language you're using, dead or otherwise, you need to learn the language to accomplish something with it and, complaining about it doesn't help you reach that goal.

Pascal, as C, C++, Python and any other language, has its own way of getting things done.  In many cases, the Pascal way is cleaner, more logical and elegant.  Realizing that requires some perseverance from its user (in this case, you.)
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.