You are not logged in.

Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

1

Friday, January 25th 2013, 12:57pm

InStr / InStrRev - Different results compared to VB6

concerns the VBA tree of the framework

Hey there,

I have noticed that Jabacos InStr() and InStrRev() Functions return different results compared to VB6!
InStr() is just a slight difference but InStrRev() might be a Bug!

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Public Sub Command1_Click()
'InStr 
   Debug.Print InStr("abab", "ab")
   If InStr("abab", "ab") <> 1 Then Stop
   Debug.Print InStr("abab", "aB")
   If InStr("abab", "aB") <> 0 Then Stop
   Debug.Print InStr(1, "abab", "aB", vbTextCompare)
   If InStr(1, "abab", "aB", vbTextCompare) <> 1 Then Stop
   Debug.Print InStr(2, "abab", "ab")
   If InStr(2, "abab", "ab") <> 3 Then Stop
   Debug.Print InStr(3, "abab", "ab")
   If InStr(3, "abab", "ab") <> 3 Then Stop
   Debug.Print InStr(4, "abab", "ab")
   If InStr(4, "abab", "ab") <> 0 Then Stop
   Debug.Print InStr(4, "aaabcab", "abc")
   If InStr(4, "aaabcab", "abc") <> 0 Then Stop
   Debug.Print InStr(4, "abab", "")
   If InStr(4, "abab", "") <> 4 Then Stop
   Debug.Print InStr(5, "abab", "")
   If InStr(5, "abab", "") <> 5 Then Stop
   Debug.Print InStr(4, "", "")
   If InStr(4, "", "") <> 0 Then Stop   'Jabaco -> 1 / VB -> 0
   Debug.Print InStr("abab", "c")
   If InStr("abab", "c") <> 0 Then Stop
   Debug.Print InStr("a" & String$(50000, "b"), "a")
   If InStr("a" & String$(50000, "b"), "a") <> 1 Then Stop
   ' unicode
   Debug.Print InStr("a€€c", "€")
   If InStr("a€€c", "€") <> 2 Then Stop
   
'InStrRev
   Debug.Print InStrRev("abab", "ab")
   If InStrRev("abab", "ab") <> 3 Then Stop
   Debug.Print InStrRev("abab", "ab", -1)
   If InStrRev("abab", "ab", -1) <> 3 Then Stop 'Jabaco -> 0 / VB -> 3
   Debug.Print InStrRev("abab", "aB")
   If InStrRev("abab", "aB") <> 0 Then Stop
   '     If InStrRev("abab", "aB", , vbTextCompare) <> 3 Then Stop 'Jabaco does not accept, in VB the argument is optional!
   Debug.Print InStrRev("abab", "aB", 1, vbTextCompare)
   If InStrRev("abab", "aB", 1, vbTextCompare) <> 0 Then Stop
   Debug.Print InStrRev("abab", "ab", 2)
   If InStrRev("abab", "ab", 2) <> 1 Then Stop
   Debug.Print InStrRev("ababc", "a", 2)
   If InStrRev("ababc", "a", 2) <> 1 Then Stop
   Debug.Print InStrRev("ababab", "ab", 3)
   If InStrRev("ababab", "ab", 3) <> 1 Then Stop 'Jabaco -> 3 / VB -> 1
   Debug.Print InStrRev("abab", "ab", 4)
   If InStrRev("abab", "ab", 4) <> 3 Then Stop
   Debug.Print InStrRev("aaabcab", "abc", 4)
   If InStrRev("aaabcab", "abc", 4) <> 0 Then Stop 'Jabaco -> 3 / VB -> 0
   Debug.Print InStrRev("abab", "", 4)
   If InStrRev("abab", "", 4) <> 4 Then Stop
   Debug.Print InStrRev("abab", "", 5)
   If InStrRev("abab", "", 5) <> 0 Then Stop
   Debug.Print InStrRev("", "", 4)
   If InStrRev("", "", 4) <> 0 Then Stop
   Debug.Print InStrRev("abab", "c")
   If InStrRev("abab", "c") <> 0 Then Stop
   Debug.Print InStrRev("a" & String$(50000, "b"), "a")
   If InStrRev("a" & String$(50000, "b"), "a") <> 1 Then Stop
   ' unicode
   Debug.Print InStrRev("a€€c", "€")
   If InStrRev("a€€c", "€") <> 3 Then Stop

End Sub


It seems like the Java function sometimes does not limit the string from the start.
Also in VB I can set -1 as the 3rd parameter sto select the whole string

Source code

1
2
3
4
'Jabaco
Debug.Print InStrRev("abababc", "c", Len("abababc"), vbTextCompare)
'VB6
Debug.Print InStrRev("abababc", "c", -1, vbTextCompare)



Dani

theuserbl

Intermediate

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

2

Monday, January 28th 2013, 10:30am

Thanks for the nice testing-proigram.

Does the following changed functions help?

Jabaco Source

1
2
3
4
5
6
7
8
public static int InStr(int Start, String String1, String String2, VBCompareMethod CompareMet) {
	if (String1.equals("")) return 0;
	switch (CompareMet.intValue()) {
		case (0): { return String1.indexOf (String2, Start - 1) + 1; }
		case (1): { return String1.toLowerCase().indexOf (String2.toLowerCase(), Start - 1) + 1; }
	}
	return String1.indexOf (String2, Start - 1) + 1;
}


Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
public static int InStrRev(String String1, String String2, int Start, VBCompareMethod CompareMet) {
	if (Start == -1) Start = String1.length();
	if (String1.length() > Start) String1=String1.substring(0,Start);
	if (String1.equals("")) return 0;
	if (Start > Len(String1)) return 0;
	switch (CompareMet.intValue()) {
		case (0): {	return String1.lastIndexOf (String2, Start - 1) + 1; }
		case (1): { return String1.toLowerCase().lastIndexOf (String2, Start - 1) + 1; }
	}
	return String1.lastIndexOf (String2, Start - 1) + 1;
}


(The functions are modified functions of that in VBA\Strings.java)

Greatings
theuserbl

theuserbl

Intermediate

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

3

Monday, January 28th 2013, 12:51pm

Updated framework
[ Jabaco-rev105.jar ]
theuserbl

Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

4

Monday, January 28th 2013, 5:38pm

:thumbsup: Very NICE theuserbl :thumbsup:

works perfect, I am glad I don't have to mess with the Java part ;)

Just to be complete on this and because people here complain about noncompatibility sometimes I'd like to mention that

Jabaco Source

1
2
Debug.Print InStrRev("abab", "a", , vbTextCompare) ' -> VB accepts the 3rd argument to be left out when the 4th is given!
Debug.Print InStrRev("abab", "a", 0)               ' -> VB throws an Error for 0


I personally don't need full compatibility; I like slim code!!


Thanks again,


Dani

Rate this thread
WoltLab Burning Board