View Issue Details

IDProjectCategoryView StatusLast Update
0000652LDMud 3.5Runtimepublic2017-09-30 18:48
ReporterGnomi Assigned ToGnomi  
PrioritynormalSeverityminorReproducibilityhave not tried
Status resolvedResolutionfixed 
Platformi686OSDebian GNU/LinuxOS Version4.0
Fixed in Version3.5.0 
Summary0000652: lvalue 4:Introduce mutable strings
DescriptionStrings are normally seen as constant, they have copy-on-write/copy-by-value semantics. Wenn creating an lvalue to a single string character the string is copied (if it has a reference count > 1). That makes it impossible to create more than one char lvalue of a string (string str="1x1"; fun(&(str[0]), &(str[2])); - it's ugly but should be possible). But it also messes strings up when the string is put into the string table again before the lvalue is used:

    string str1,str2;
    mixed c;
    
    str1="abcdefgh";
    c = (0||&(str1[3]));

    c='1';
    str2=str1; /* str2 should be "abc1efgh"; */
    c='2'; /* should only change str1. */

    return ({str1,str2});

returns ({"abc2efgh","abc2efgh"}).

This also messes up string hashing:

    mapping m = ([:1]);
    string str; mixed c;
    
    str="abcdefghi";
    c = (0||&(str[3]));
    c='1';
    
    m[str] = "Nr. 1";
    c='2';
    m["abc2efghi"] = "Nr. 2";
    return m;

returns (["abc2efghi":"Nr. 2","abc2efghi":"Nr. 1"]) which is quite unusual for mappings (having a key twice).

So my idea is to create a special type for mutable strings, so that they can have more than one reference to it. That would also prevent char lvalues from getting invalid, because such a string would exist as long as references to it exist.

They should be another subtype of strings. (There already are tabled and non-tabled but also constant strings). Mutable strings are strings that can be changed in-place. An untabled string can be changed into a mutable one if it has a refcount of 1. A mutable string can be made constant again if it has a refcount of 1 and if this refcounts comes from a svalue structure (and not from an lvalue).

Whenever a mutable string is pushed as an rvalue on the stack it is changed if possible or copied into a constant string.

To mark a string as mutable, one of the refcounting bits can be used. (2^30 references to a string should still be enough...)

Mutable strings still have a constant length (as have vectors), length-changing replacements (str[1..1]="abc") create a new string.
TagsNo tags attached.

Relationships

child of 0000546 resolvedGnomi Rework lvalue handling 

Activities

Gnomi

2009-06-09 10:21

manager   ~0001196

This might not be necessary. Char lvalues just have to behave different. Now they store a pointer (char*) to the char and keep a reference to the full string. Instead we need a reference to the string, the local variable (which has to be a protected lvalue) and the index. When assigning a value to the local variable, change the string, copy the string when necessary (tabled or more than one reference) and when doing so, update the variable lvalue to point to the new string.

Issue History

Date Modified Username Field Change
2009-06-03 14:53 Gnomi New Issue
2009-06-03 14:53 Gnomi Status new => assigned
2009-06-03 14:53 Gnomi Assigned To => Gnomi
2009-06-03 14:53 Gnomi Relationship added child of 0000546
2009-06-09 10:21 Gnomi Note Added: 0001196
2017-09-30 18:48 Gnomi Status assigned => resolved
2017-09-30 18:48 Gnomi Resolution open => fixed
2017-09-30 18:48 Gnomi Fixed in Version => 3.5.0