5

Here's the deal. I am doing some string manipulation and I am using the substr method often. However, the way I need to use it, is more like a php fread method. Whereas, my substr needs to be guided by a pointer. The process needs to act like this:

var string='Loremipsumdolorsitamet,consectetur'

and if I read in, 'Lorem'.....as my first substr call as such:

string.substr(offset,strLenth)//0,5

then my next substr call should automatically start with an offset starting at this position in my string:

offset pointer starts here now=>ipsumdolorsitamet,consectetur'

If you haven't noticed, the offset needs to know, to start at the sixth position in the string.

Soooo... I came up with this working solution, and I want to know if it is a good solution or if anyone has any recommendations to add to it?:

var _offSetPointer=0

var _substrTest={
    _offset:function(){return _offSetPointer+=getLength}
    };  


//usage, where p is the length in bytes of the substring you want to capture.
string.substr(_substrTest._offset(getLength=p),p)
cube
  • 1,774
  • 4
  • 23
  • 33

1 Answers1

10
var reader = function (string, offset) {
    offset = offset || 0;
    return function (n) {
        return string.substring(offset, (offset+=n||1));
    }
}

var read = reader("1234567");

read(2)
"12"

read(3)
"345"

read()
"6"

read(1)
"7"
Cristian Sanchez
  • 31,171
  • 11
  • 57
  • 63
  • +1 very nice Daniel! Should that offset be defined with `var` as well to scope it to that function? – alex Sep 21 '10 at 00:18
  • 1
    @alex: Thanks! Doesn't need to be declared using `var` since it was introduced into the scope as an argument. If you provide the offset it will start at that position when you call read otherwise it defaults to 0 (the beginning of the string). – Cristian Sanchez Sep 21 '10 at 00:21
  • 1
    Nice use of closures and currying. – Peter Ajtai Sep 21 '10 at 00:23
  • Daniel, please help me understand your code, as I am just learning javascript. How does the reader function know what is suppose to be the first and second arguments? If i do, read(7), how does know the 7 is not the string parameter? – cube Sep 21 '10 at 00:32
  • @cube Look carefully at what it returns. The function returned is what is called in the examples below. Because of the [closure](http://www.jibbering.com/faq/notes/closures/) though, that function has access to `string`. – alex Sep 21 '10 at 00:40
  • 2
    @cube: Sure. There is a distinction between `reader` and `read`. `reader` **returns a function**. When I did `read = reader(...)` it assigned that function to the read variable. In Javascript, functions maintain a reference to all variables (sans `this` and `arguments`) that are available to them when they are defined. In this case, it maintains access to the `offset` variable. And everytime I call the `read` function, it increments that variable. So the next time the `read` function is used, it will use that *same* offset variable (which may have been incremented previously). – Cristian Sanchez Sep 21 '10 at 00:40
  • I needed a reference to offset. So i modified Daniel's code as such:
    – cube Sep 22 '10 at 11:40
  • I needed a reference to offset. So i modified Daniel's code as such: >var string=string.substr(offset, (offset+=n||1));return {string:string,offset:offset}< I can now reference the objects string, as well as offset(where the pointer is, a la php's ftell()). btw, how the hell do you highlight code in these comments!?...anyway, I LUV THIS SITE!!!!!, Thanks Daniel! – cube Sep 22 '10 at 11:48