diff --git a/src/sgfParser/__init__.py b/src/sgfParser/__init__.py --- a/src/sgfParser/__init__.py +++ b/src/sgfParser/__init__.py @@ -4,23 +4,25 @@ def skipWhitespace(s,start): return i -def lineNumber(s,i): +def strRowCol(s, i): k=0 - r=0 + r,c=0,0 for (r,line) in enumerate(s.splitlines(True)): - k+=len(line) - if k>=i: break - return r+1 + c=i-k + if k+len(line)>i: + break + else: + k+=len(line) + return (r+1, c+1) class ParserError(Exception): - def __init__(self,message,s,i): - # self.line=line - # self.col=col - # !! check for i=len(s) or s[i]!=")": - raise ParserError("expected end of the GameTree marked by ')'",s,i) + raise ParserError("expected end of a GameTree marked by ')'",s,i) i=skipWhitespace(s,i+1) return (i,res) diff --git a/src/sgfParser/property.py b/src/sgfParser/property.py --- a/src/sgfParser/property.py +++ b/src/sgfParser/property.py @@ -85,7 +85,7 @@ def compose(vTypeA,vTypeB): def f(s,start): i,a=vTypeA(s,start) if i>=len(s) or s[i]!=":": - raise ParserError("a composed property value separated by ':' expected",s,i) + raise ParserError("expected a composed property value separated by ':'",s,i) i,b=vTypeB(s,i+1) return (i,Composed(a,b)) return f diff --git a/src/tests/testSgfParser.py b/src/tests/testSgfParser.py --- a/src/tests/testSgfParser.py +++ b/src/tests/testSgfParser.py @@ -1,7 +1,9 @@ +from itertools import chain import unittest from unittest import TestCase import os +from sgfParser import strRowCol from sgfParser.collection import Collection from sgfParser.property import Property @@ -9,6 +11,21 @@ from sgfParser.property import Property dataDir=os.path.join(os.path.dirname(__file__), "data") +class TestUtils(TestCase): + def testTextPos(self): + s="abc\ndef\rgh\r\nij\n\rklmn" + rc=[ + [1,2,3,4], + [1,2,3,4], + [1,2,3,4], + [1,2,3], [1], # don't care about LFCR, we unicode now + [1,2,3,4] + ] + res=chain((r+1,c) for (r,row) in enumerate(rc) for c in row) + for (i,(r,c)) in zip(range(len(s)+1), res): + self.assertEqual(strRowCol(s, i), (r, c)) + + class TestProperty(TestCase): def testName(self): with self.assertRaises(AssertionError):