RE : Re: [haXe] Property access on strings like in JavaScript / PHP ?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

RE : Re: [haXe] Property access on strings like in JavaScript / PHP ?

David Elahee

Hi, you are touching the limit between nice and dangerous :-).

My 2 cent.
David

Le 3 juil. 2011 21:35, "Joe List" <[hidden email]> a écrit :

Oh yes sorry, I forgot to say with brackets.

There is property bracket access on strings in haXe ?

Like in JavaScript/PHP

In ruby you can make substing "foobar"[1..3] == "oob"

It's really nice.

Seems not possible in haXe.



2011/7/3 Julien CASTETS <[hidden email]>
>
> Hi,
>
> I think you could do "foobar".charAt...


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: RE : Re: [haXe] Property access on strings like in JavaScript / PHP ?

laurence taylor
It's incomplete, but here is an example on how to implement ruby flavour string ops. see the attachment for a buildable project,

//----------------------------------------------------------------------------------------------
using RubyString;

class Main {
static function main() {
var b = "foobar".replace([0, 2], "chu");
trace(b);
var c = b.replace([3, 5], "bac");
trace(c);
var d = c.replace([2, 3], "du");
trace(d);
var e = c.replace([0], "h");
trace(e);
trace(  "foobar".range([1, 3]) );
}
}

//-------------------------------------------------------------------------------------------------
import haxe.macro.Expr;
import haxe.macro.Context;
import haxe.macro.Type;
import haxe.macro.Compiler;


class RubyString  {

@:macro public static function replace(e:Expr, a:Array<Int>, r:String) {
if (a.length == 1) {
a[1] = a[0];
}
#if debug
if (a.length != 2 ) {
throw "Invalid range spec";
}
if (a[1] < a[0]) {
throw "Doesn't support reverse substitution";
}
if ( (a[1] - a[0]) != (r.length - 1) ) {
throw "Substitution string must be of the same length as range";
}
#end
var fn = "function(s:String):String {
var left = s.substr(0, " + a[0] + ");
var right = s.substr(" + a[1] + " +1);
return left +  \"" + r + "\" + right;
}";
var str : String = null;
var expr = null;
var p = Context.currentPos();
switch( e.expr ) {
case EConst(c) :
switch(c) {
case CString(s) :
expr = Context.parse(fn + "(\"" + s + "\")",p);
case CIdent(c) :
expr = Context.parse(fn + "(" + c + " )",p);
default : throw "Expression must be either a String or an Identifier";
}
default : throw "Expression must be either a String or an Identifier";
}
return expr;
}
@:macro public static function range(e:Expr, a:Array<Int>) {
if (a.length == 1) {
a[1] = a[0];
}
#if debug
if (a.length != 2 ) {
throw "Invalid range spec.";
}
if (a[1] < a[0]) {
throw "Doesn't support String reversal.";
}
#end
var fn = "function(s:String):String {
return s.substr(" + a[0] + "," + a[1] + ");
}";
var str : String = null;
var expr = null;
var p = Context.currentPos();
switch( e.expr ) {
case EConst(c) :
switch(c) {
case CString(s) :
expr = Context.parse(fn + "(\"" + s + "\")",p);
case CIdent(c) :
expr = Context.parse(fn + "(" + c + " )",p);
default : throw "Expression must be either a String or an Identifier";
}
default : throw "Expression must be either a String or an Identifier";
}
return expr;
}
}

--
haXe - an open source web programming language
http://haxe.org

RubyStringRange.7z (25K) Download Attachment