zephir icon indicating copy to clipboard operation
zephir copied to clipboard

Get char from a string

Open mruz opened this issue 4 years ago • 3 comments

$test = "test";
$t = $test[1];
var_dump($t);

string(1) "e"

zephir

char t;
string test = "test";
let t = test[1];
var_dump(t);
ZEPHIR_INIT_VAR(&test);
ZVAL_STRING(&test, "test");
t = ZEPHIR_STRING_OFFSET(&test, 1);
t = t;
ZEPHIR_INIT_VAR(&_10);
ZEPHIR_INIT_NVAR(&_10);
ZVAL_LONG(&_10, t);
zephir_var_dump(&_10);

int(101)

mruz avatar Mar 15 '20 16:03 mruz

A cast char to string workaround:

char t;
string test = "test";
let t = test[1];

var s;
let s = (string) t;

but it's painful if I want to do ord(data[0]), we could play with chr(), but @sergeyklay maybe there is a better way, is it related to https://github.com/phalcon/zephir/issues/1988?

I think it works if we do it in the loop, but I have to double-check:

int i;
char c;
string test = "test";

for i, c in test {
    var_dump(c, test[i]);
}

mruz avatar Mar 26 '20 11:03 mruz

@mruz Thank you for the bug report. I'll try to sort out

sergeyklay avatar Mar 26 '20 14:03 sergeyklay

And yes, this is related to #1988

sergeyklay avatar Mar 26 '20 14:03 sergeyklay