[sword-svn] r2785 - in trunk: include src/keys src/modules/texts/rawtext src/modules/texts/ztext tests tests/testsuite

scribe at crosswire.org scribe at crosswire.org
Sat Feb 23 16:53:35 MST 2013


Author: scribe
Date: 2013-02-23 16:53:35 -0700 (Sat, 23 Feb 2013)
New Revision: 2785

Modified:
   trunk/include/versekey.h
   trunk/src/keys/versekey.cpp
   trunk/src/modules/texts/rawtext/rawtext.cpp
   trunk/src/modules/texts/ztext/ztext.cpp
   trunk/tests/parsekey.cpp
   trunk/tests/testsuite/versekeytest.good
   trunk/tests/versekeytest.cpp
Log:
added ability to ++ and -- and normalize with intros on
better handling of ++ and -- with autonorm off
added more tests
fixed bug with HunKar module (and others) with [ Testament 1 Intro ] not parseable when skipConsecutiveLinks was on.
turned make autonormalize settings in ListKey results from VerseKey::parseVerseList equal to the VerseKey isAutoNormalize value.



Modified: trunk/include/versekey.h
===================================================================
--- trunk/include/versekey.h	2013-02-23 16:04:02 UTC (rev 2784)
+++ trunk/include/versekey.h	2013-02-23 23:53:35 UTC (rev 2785)
@@ -75,15 +75,7 @@
 	// private with no bounds check
 	void setFromOther(const VerseKey &vk);
 
-	/** Binary search to find the index closest, but less
-	* than the given value.
-	*
-	* @param array long * to array to search
-	* @param size number of elements in the array
-	* @param value value to find
-	* @return the index into the array that is less than but closest to value
-	*/
-	int findindex(long *array, int size, long value);
+	void checkBounds();
 
 	// internal upper/lower bounds optimizations
 	mutable long lowerBound, upperBound;	// if autonorms is on

Modified: trunk/src/keys/versekey.cpp
===================================================================
--- trunk/src/keys/versekey.cpp	2013-02-23 16:04:02 UTC (rev 2784)
+++ trunk/src/keys/versekey.cpp	2013-02-23 23:53:35 UTC (rev 2785)
@@ -710,6 +710,7 @@
 					lastKey->setLowerBound(*curKey);
 					lastKey->setPosition(TOP);
 					tmpListKey << *lastKey;
+					((VerseKey *)tmpListKey.GetElement())->setAutoNormalize(isAutoNormalize());
 					tmpListKey.GetElement()->userData = (__u64)(bufStart+(buf-iBuf.c_str()));
 				}
 				else {
@@ -723,6 +724,7 @@
 							lastKey->setUpperBound(*curKey);
 							*lastKey = TOP;
 							tmpListKey << *lastKey;
+							((VerseKey *)tmpListKey.GetElement())->setAutoNormalize(isAutoNormalize());
 							tmpListKey.GetElement()->userData = (__u64)(bufStart+(buf-iBuf.c_str()));
 						}
 						else {
@@ -737,6 +739,7 @@
 							lastKey->setUpperBound(*curKey);
 							*lastKey = TOP;
 							tmpListKey << *lastKey;
+							((VerseKey *)tmpListKey.GetElement())->setAutoNormalize(isAutoNormalize());
 							tmpListKey.GetElement()->userData = (__u64)(bufStart+(buf-iBuf.c_str()));
 						}
 					}
@@ -1212,13 +1215,16 @@
 		break;
 	}
 	case POS_MAXVERSE:
+		suffix    = 0;
+		verse     = 1;
 		normalize();
 		verse     = getVerseMax();
 		suffix    = 0;
 		break;
 	case POS_MAXCHAPTER:
-		verse     = 1;
 		suffix    = 0;
+		verse     = 1;
+		chapter   = 1;
 		normalize();
 		chapter   = getChapterMax();
 		break;
@@ -1247,6 +1253,12 @@
  */
 
 void VerseKey::increment(int step) {
+	// if we're not autonormalizing and we're already not normalized
+	if (!autonorm && chapter > 0 && verse > getVerseMax()) {
+		verse += step;
+		checkBounds();
+		return;
+	}
 	char ierror = 0;
 	setIndex(getIndex() + step);
 	while ((!verse) && (!intros) && (!ierror)) {
@@ -1267,8 +1279,13 @@
  */
 
 void VerseKey::decrement(int step) {
+	// if we're not autonormalizing and we're already not normalized
+	if (!autonorm && chapter > 0 && verse > getVerseMax()) {
+		verse -= step;
+		checkBounds();
+		return;
+	}
 	char ierror = 0;
-
 	setIndex(getIndex() - step);
 	while ((!verse) && (!intros) && (!ierror)) {
 		setIndex(getIndex() - 1);
@@ -1291,100 +1308,81 @@
 void VerseKey::normalize(bool autocheck)
 {
 
-	if (((!autocheck) || (autonorm))	// only normalize if we were explicitely called or if autonorm is turned on
-               &&
-	          ((!intros) || ((verse) && (chapter)))) {		// this is cheeze and temporary until deciding what actions should be taken; so intros should only be turned on when positioning with setIndex() or incrementors
+	if ((!autocheck || autonorm)	// only normalize if we were explicitely called or if autonorm is turned on
+	) {
 		error = 0;
 
-          while ((testament < 3) && (testament > 0)) {
+		while ((testament < 3) && (testament > 0)) {
 
-               if (book > BMAX[testament-1]) {
-                    book -= BMAX[testament-1];
-                    testament++;
-                    continue;
-               }
 
-               if (book < 1) {
-                    if (--testament > 0) {
-                         book += BMAX[testament-1];
-                    }
-                    continue;
-               }
+			if (book > BMAX[testament-1]) {
+				book -= (BMAX[testament-1] + (intros?1:0));
+				testament++;
+				continue;
+			}
+			if (book < (intros?0:1)) {
+				if (--testament > 0) {
+					book += (BMAX[testament-1] + (intros?1:0));
+				}
+				continue;
+			}
 
-               if (chapter > getChapterMax()) {
-                    chapter -= getChapterMax();
-                    book++;
-                    continue;
-               }
 
-		if (chapter < 1) {
-			if (--book > 0) {
-				chapter += getChapterMax();
-				verse = getVerseMax();
+			if (chapter > getChapterMax()) {
+				chapter -= (getChapterMax() + (intros?1:0));
+				book++;
+				continue;
 			}
-			else {
-				if (testament > 1) {
-					chapter += refSys->getBook(BMAX[0]-1)->getChapterMax();
-					verse = refSys->getBook(BMAX[0]-1)->getVerseMax(chapter);
-				}
+			if (chapter < (intros?0:1)) {
+				--book;
+				chapter += (getChapterMax() + (intros?1:0));
+				continue;
 			}
-			continue;
-		}
 
-               if (verse > getVerseMax()) { // -1 because e.g chapter 1 of Matthew is books[1][0].versemax[0]
-                    verse -= getVerseMax();
+
+			if (chapter > 0 && verse > getVerseMax()) {
+				verse -= (getVerseMax() + (intros?1:0));
 				chapter++;
-                    continue;
-               }
+				continue;
+			}
+			if (verse < (intros?0:1)) {
+				if (--chapter < (intros?0:1)) {
+					--book;
+					chapter += (getChapterMax() + (intros?1:0));
+				}
+				verse += (getVerseMax() + (intros?1:0));
+				continue;
+			}
 
-               if (verse < 1) {
-                    if (--chapter > 0) {
-                         verse += getVerseMax();
-                    }
-                    else	{
-                         if (book > 1) {
-						const VersificationMgr::Book *prevBook = refSys->getBook(((testament>1)?BMAX[0]:0)+book-2);
-                              verse += prevBook->getVerseMax(prevBook->getChapterMax());
-                         }
-                         else	{
-                              if (testament > 1) {
-							const VersificationMgr::Book *lastOTBook = refSys->getBook(BMAX[0]-1);
-                                   verse += lastOTBook->getVerseMax(lastOTBook->getChapterMax());
-                              }
-                         }
-                    }
-                    continue;
-               }
+			break;  // If we've made it this far (all failure checks continue) we're ok
+		}
 
-               break;  // If we've made it this far (all failure checks continue) we're ok
-          }
+		if (testament > (BMAX[1]?2:1)) {
+			testament = BMAX[1]?2:1;
+			book      = BMAX[testament-1];
+			chapter   = getChapterMax();
+			verse     = getVerseMax();
+			error     = KEYERR_OUTOFBOUNDS;
+		}
 
-          if (testament > (BMAX[1]?2:1)) {
-               testament = BMAX[1]?2:1;
-               book      = BMAX[testament-1];
-               chapter   = getChapterMax();
-               verse     = getVerseMax();
-               error     = KEYERR_OUTOFBOUNDS;
-          }
+		if (testament < 1) {
+			error     = ((!intros) || (testament < 0) || (book < 0)) ? KEYERR_OUTOFBOUNDS : 0;
+			testament = ((intros) ? 0 : 1);
+			book      = ((intros) ? 0 : 1);
+			chapter   = ((intros) ? 0 : 1);
+			verse     = ((intros) ? 0 : 1);
+		}
 
-          if (testament < 1) {
-               error     = ((!intros) || (testament < 0) || (book < 0)) ? KEYERR_OUTOFBOUNDS : 0;
-               testament = ((intros) ? 0 : 1);
-               book      = ((intros) ? 0 : 1);
-               chapter   = ((intros) ? 0 : 1);
-               verse     = ((intros) ? 0 : 1);
-          }
-
-          // should we always perform bounds checks?  Tried but seems to cause infinite recursion
-          if (_compare(getUpperBound()) > 0) {
-               positionFrom(getUpperBound());
-               error = KEYERR_OUTOFBOUNDS;
-          }
-          if (_compare(getLowerBound()) < 0) {
-               positionFrom(getLowerBound());
-               error = KEYERR_OUTOFBOUNDS;
-          }
-     }
+			// should we always perform bounds checks?  Tried but seems to cause infinite recursion
+		if (_compare(getUpperBound()) > 0) {
+			positionFrom(getUpperBound());
+			error = KEYERR_OUTOFBOUNDS;
+		}
+		if (_compare(getLowerBound()) < 0) {
+			positionFrom(getLowerBound());
+			error = KEYERR_OUTOFBOUNDS;
+		}
+	}
 }
 
 
@@ -1442,16 +1440,16 @@
  * ENT:	itestament - value which to set testament
  *		[MAXPOS(char)] - only get
  *
- * RET:	if unchanged ->          value of testament
- *	if   changed -> previous value of testament
  */
 
 void VerseKey::setTestament(char itestament)
 {
-	if (itestament != MAXPOS(char)) {
-		testament = itestament;
-		normalize(true);
-	}
+	suffix  = 0;
+	verse   = (intros) ? 0 : 1;
+	chapter = (intros) ? 0 : 1;
+	book    = (intros) ? 0 : 1;
+	testament = itestament;
+	normalize(true);
 }
 
 
@@ -1463,8 +1461,9 @@
 
 void VerseKey::setBook(char ibook)
 {
-	verse   = 1;
-	chapter = 1;
+	suffix  = 0;
+	verse   = (intros) ? 0 : 1;
+	chapter = (intros) ? 0 : 1;
 	book    = ibook;
 	normalize(true);
 }
@@ -1500,11 +1499,10 @@
 
 void VerseKey::setChapter(int ichapter)
 {
-	verse   = 1;
+	suffix  = 0;
+	verse   = (intros) ? 0 : 1;
 	chapter = ichapter;
 	normalize(true);
-	// TODO: easiest fix, but should be in normalize
-	verse   = 1;
 }
 
 
@@ -1520,8 +1518,8 @@
 
 void VerseKey::setVerse(int iverse)
 {
-	setSuffix(0);
-	verse = iverse;
+	suffix  = 0;
+	verse   = iverse;
 	normalize(true);
 }
 
@@ -1570,33 +1568,7 @@
 	return intros;
 }
 
-/******************************************************************************
- * VerseKey::findindex - binary search to find the index closest, but less
- *						than the given value.
- *
- * ENT:	array	- long * to array to search
- *		size		- number of elements in the array
- *		value	- value to find
- *
- * RET:	the index into the array that is less than but closest to value
- */
 
-int VerseKey::findindex(long *array, int size, long value)
-{
-	int lbound, ubound, tval;
-
-	lbound = 0;
-	ubound = size - 1;
-	while ((ubound - lbound) > 1) {
-		tval = lbound + (ubound-lbound)/2;
-		if (array[tval] <= value)
-			lbound = tval;
-		else ubound = tval;
-	}
-	return (array[ubound] <= value) ? ubound : lbound;
-}
-
-
 /******************************************************************************
  * VerseKey::getIndex - Gets index based upon current verse
  *
@@ -1660,6 +1632,11 @@
 	if (book < 0) { testament = 0; book = 0; }
 	if (chapter < 0) { book = 0; chapter = 0; }
 
+	checkBounds();
+}
+
+void VerseKey::checkBounds() {
+
 	long i = getIndex();
 
 	initBounds();

Modified: trunk/src/modules/texts/rawtext/rawtext.cpp
===================================================================
--- trunk/src/modules/texts/rawtext/rawtext.cpp	2013-02-23 16:04:02 UTC (rev 2784)
+++ trunk/src/modules/texts/rawtext/rawtext.cpp	2013-02-23 23:53:35 UTC (rev 2785)
@@ -157,11 +157,15 @@
 		}
 		long index = tmpkey->getTestamentIndex();
 		findOffset(tmpkey->getTestament(), index, &start, &size);
+
 		if (
-			(((laststart != start) || (lastsize != size))	// we're a different entry
+			(
+				((laststart != start) || (lastsize != size))	// we're a different entry
 //				&& (start > 0)
-				&& (size))	// and we actually have a size
-				||(!skipConsecutiveLinks)) {	// or we don't want to skip consecutive links
+				&& (size)	// and we actually have a size
+			)
+			|| !skipConsecutiveLinks
+		) {	// or we don't want to skip consecutive links
 			steps += (steps < 0) ? 1 : -1;
 			lastgood = *tmpkey;
 		}
@@ -169,6 +173,7 @@
 	error = (error) ? KEYERR_OUTOFBOUNDS : 0;
 }
 
+
 bool RawText::isLinked(const SWKey *k1, const SWKey *k2) const {
 	long start1, start2;
 	unsigned short size1, size2;

Modified: trunk/src/modules/texts/ztext/ztext.cpp
===================================================================
--- trunk/src/modules/texts/ztext/ztext.cpp	2013-02-23 16:04:02 UTC (rev 2784)
+++ trunk/src/modules/texts/ztext/ztext.cpp	2013-02-23 23:53:35 UTC (rev 2785)
@@ -182,10 +182,13 @@
 		findOffset(tmpkey->getTestament(), index, &start, &size, &buffnum);
 
 		if (
-			(((laststart != start) || (lastsize != size))	// we're a different entry
+			(
+				((laststart != start) || (lastsize != size))	// we're a different entry
 //				&& (start > 0)
-				&& (size))	// and we actually have a size
-				||(!skipConsecutiveLinks)) {	// or we don't want to skip consecutive links
+				&& (size)	// and we actually have a size
+			)
+			|| !skipConsecutiveLinks
+		) {	// or we don't want to skip consecutive links
 			steps += (steps < 0) ? 1 : -1;
 			lastgood = *tmpkey;
 		}
@@ -193,6 +196,7 @@
 	error = (error) ? KEYERR_OUTOFBOUNDS : 0;
 }
 
+
 bool zText::isLinked(const SWKey *k1, const SWKey *k2) const {
 	long start1, start2;
 	unsigned short size1, size2;

Modified: trunk/tests/parsekey.cpp
===================================================================
--- trunk/tests/parsekey.cpp	2013-02-23 16:04:02 UTC (rev 2784)
+++ trunk/tests/parsekey.cpp	2013-02-23 23:53:35 UTC (rev 2785)
@@ -26,8 +26,8 @@
 #endif
 
 int main(int argc, char **argv) {
-	if ((argc < 2) || (argc > 7)) {
-		std::cerr << "usage: " << *argv << " <\"string to parse\"> [locale_name] [v11n] [context] [echo params 1|0] [test-in-set-verse]\n";
+	if ((argc < 2) || (argc > 8)) {
+		std::cerr << "usage: " << *argv << " <\"string to parse\"> [locale_name] [v11n] [context] [echo params 1|0] [test-in-set-verse 1|0] [intros 1|0]\n";
 		exit(-1);
 	}
 
@@ -42,14 +42,20 @@
 
 	const char *context = (argc > 4) ? argv[4] : "gen.1.1";
 
+	bool echo = (argc > 5) ? !strcmp(argv[5], "1") : false;
+	bool inSetTest = (argc > 6) ? !strcmp(argv[6], "1") : false;
+	bool intros = (argc > 7) ? !strcmp(argv[7], "1") : false;
+
+	DefaultVSKey.setIntros(intros);
+
 	SWLog::getSystemLog()->setLogLevel(SWLog::LOG_DEBUG);
 	DefaultVSKey.validateCurrentLocale();
 
 	DefaultVSKey = context;
-	
+
 	ListKey verses = DefaultVSKey.parseVerseList(argv[1], DefaultVSKey, true);
 
-	if ((argc > 5) && (argv[5][0] == '1')) {
+	if (echo) {
 		for (int i = 1; i < argc; i++) {
 			if (i > 1) std::cout << " ";
 			std::cout << argv[i];
@@ -63,8 +69,8 @@
 	}
 */
 
-	if (argc > 6) {
-		verses.setText(argv[4]);
+	if (inSetTest) {
+		verses.setText(context);
 		std::cout << "Verse is" << ((verses.popError()) ? " NOT" : "") << " in set.\n\n";
 	}
 	

Modified: trunk/tests/testsuite/versekeytest.good
===================================================================
--- trunk/tests/testsuite/versekeytest.good	2013-02-23 16:04:02 UTC (rev 2784)
+++ trunk/tests/testsuite/versekeytest.good	2013-02-23 23:53:35 UTC (rev 2785)
@@ -1,3 +1,9 @@
+Should be: [ Testament 1 Heading ]
+[ Testament 1 Heading ]
+
+Should be: [ Testament 1 Heading ]
+[ Testament 1 Heading ]
+
 single
 multiple
 x: Amos 1:5
@@ -10,6 +16,9 @@
 
 Normalization on; headings on ====
 
+jn.3.50: John 4:13
+++: John 4:14
+--: John 4:13
 MAXVERSE: John 4:54
 MAXCHAPTER: John 21:1
 TOP: [ Module Heading ]
@@ -18,6 +27,9 @@
 Normalization off; headings on ====
 
 jn.3.50: John 3:50
+++: John 3:51
+--: John 3:50
+MAXVERSE: John 3:36
 MAXCHAPTER: John 21:1
 TOP: [ Module Heading ]
 BOTTOM: Revelation of John 22:21
@@ -35,6 +47,9 @@
 Normalization off; headings off ====
 
 jn.3.50: John 3:50
+++: John 3:51
+--: John 3:50
+MAXVERSE: John 3:36
 MAXCHAPTER: John 21:1
 TOP: Genesis 1:1
 BOTTOM: Revelation of John 22:21

Modified: trunk/tests/versekeytest.cpp
===================================================================
--- trunk/tests/versekeytest.cpp	2013-02-23 16:04:02 UTC (rev 2784)
+++ trunk/tests/versekeytest.cpp	2013-02-23 23:53:35 UTC (rev 2785)
@@ -79,7 +79,16 @@
 	SWModule *mod = mgr.getModule("KJVgb");
 */
 	VerseKey *parser = new VerseKey(); //(VerseKey *)mod->CreateKey();
+	parser->setIntros(true);
 
+	ListKey result = parser->parseVerseList("[ Testament 1 Heading ]");
+	cout << "Should be: [ Testament 1 Heading ]\n" << result << "\n\n";
+
+	parser->setText("[ Testament 1 Heading ]");
+	cout << "Should be: [ Testament 1 Heading ]\n" << *parser << "\n\n";
+
+	result.clear();
+
 	ListKey scope = parser->parseVerseList("amos 2:2", *parser, true);
 
 	cout << ((scope++ == scope) ? "single" : "multiple") << "\n";
@@ -90,8 +99,6 @@
 
 	scope = parser->parseVerseList("amos", *parser, true);
 
-	ListKey result;
-
 	scope++;
 	scope++;
 	scope++;
@@ -128,7 +135,7 @@
 
      *x = "Is.1.13";
      scope = *x;
-//     if (scope == x) std::cout << "Error restricting bounds: " << x.getText() << " is in " << bounds << "\n";
+     if (scope == *x) std::cout << "Error restricting bounds: " << x->getText() << " is in " << bounds << "\n";
      if (!scope.popError()) std::cout << "Error restricting bounds: " << x->getText() << " is in " << bounds << "\n";
 
      *x = "1Sam.21.1";
@@ -160,13 +167,11 @@
 	vk.setIntros(true);
 
 	vk = "jn3.50";
-/* TODO: these report 1 number off from what I think they should report 
 	cout << "jn.3.50: " << vk << "\n";
 	vk++;
 	cout << "++: " << vk << "\n";
 	vk--;
 	cout << "--: " << vk << "\n";
-*/
 	vk = MAXVERSE;
 	cout << "MAXVERSE: " << vk << "\n";
 	vk = MAXCHAPTER;
@@ -183,14 +188,12 @@
 
 	vk = "jn3.50";
 	cout << "jn.3.50: " << vk << "\n";
-/* TODO: These probably are undefined and likely to be ok
 	vk++;
 	cout << "++: " << vk << "\n";
 	vk--;
 	cout << "--: " << vk << "\n";
 	vk = MAXVERSE;
 	cout << "MAXVERSE: " << vk << "\n";
-*/
 	vk = MAXCHAPTER;
 	cout << "MAXCHAPTER: " << vk << "\n";
 	vk = TOP;
@@ -225,14 +228,12 @@
 
 	vk = "jn3.50";
 	cout << "jn.3.50: " << vk << "\n";
-/* TODO: These probably are undefined and likely to be ok
 	vk++;
 	cout << "++: " << vk << "\n";
 	vk--;
 	cout << "--: " << vk << "\n";
 	vk = MAXVERSE;
 	cout << "MAXVERSE: " << vk << "\n";
-*/
 	vk = MAXCHAPTER;
 	cout << "MAXCHAPTER: " << vk << "\n";
 	vk = TOP;
@@ -246,7 +247,7 @@
 
 	VerseKey vkey;
 	VerseKey tmpkey = "1sam 1:1";
-	vkey.setAutoNormalize(true);                          // line 147
+	vkey.setAutoNormalize(true);
 	vkey = tmpkey;
 	int chapter = (vkey.getChapter()-1);
 	vkey.setChapter(chapter);




More information about the sword-cvs mailing list