Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
flatbuffers
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
flatbuffers
Commits
7c69c5dc
Commit
7c69c5dc
authored
8 years ago
by
TGIshib
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix lookupByKey, improve compareStrings
parent
9f16090f
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
30 additions
and
27 deletions
+30
-27
Table.java
java/com/google/flatbuffers/Table.java
+11
-13
Table.cs
net/FlatBuffers/Table.cs
+9
-11
idl_gen_general.cpp
src/idl_gen_general.cpp
+6
-1
Monster.cs
tests/MyGame/Example/Monster.cs
+2
-1
Monster.java
tests/MyGame/Example/Monster.java
+2
-1
No files found.
java/com/google/flatbuffers/Table.java
View file @
7c69c5dc
...
...
@@ -235,35 +235,33 @@ public class Table {
int
startPos_1
=
offset_1
+
SIZEOF_INT
;
int
startPos_2
=
offset_2
+
SIZEOF_INT
;
int
len
=
Math
.
min
(
len_1
,
len_2
);
byte
[]
bbArray
=
bb
.
array
();
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
if
(
bb
.
array
()[
i
+
startPos_1
]
!=
bb
.
array
()
[
i
+
startPos_2
])
return
bb
.
array
()[
i
+
startPos_1
]
-
bb
.
array
()
[
i
+
startPos_2
];
if
(
bb
Array
[
i
+
startPos_1
]
!=
bbArray
[
i
+
startPos_2
])
return
bb
Array
[
i
+
startPos_1
]
-
bbArray
[
i
+
startPos_2
];
}
if
(
len_1
<
len_2
)
return
-
1
;
if
(
len_1
>
len_2
)
return
1
;
return
0
;
return
len_1
-
len_2
;
}
/**
* Compare string from the buffer with the 'String' object.
*
* @param offset_1 An 'int' index of the first string into the bb.
* @param key Second string.
* @param key Second string
as a byte array
.
* @param bb A {@code ByteBuffer} to get the first string.
*/
protected
static
int
compareStrings
(
int
offset_1
,
String
key
,
ByteBuffer
bb
)
{
protected
static
int
compareStrings
(
int
offset_1
,
byte
[]
key
,
ByteBuffer
bb
)
{
offset_1
+=
bb
.
getInt
(
offset_1
);
int
len_1
=
bb
.
getInt
(
offset_1
);
int
len_2
=
key
.
length
()
;
int
len_2
=
key
.
length
;
int
startPos_1
=
offset_1
+
Constants
.
SIZEOF_INT
;
int
len
=
Math
.
min
(
len_1
,
len_2
);
byte
[]
bbArray
=
bb
.
array
();
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
if
(
bb
.
array
()[
i
+
startPos_1
]
!=
key
.
charAt
(
i
)
)
return
bb
.
array
()[
i
+
startPos_1
]
-
key
.
charAt
(
i
)
;
if
(
bb
Array
[
i
+
startPos_1
]
!=
key
[
i
]
)
return
bb
Array
[
i
+
startPos_1
]
-
key
[
i
]
;
}
if
(
len_1
<
len_2
)
return
-
1
;
if
(
len_1
>
len_2
)
return
1
;
return
0
;
return
len_1
-
len_2
;
}
}
...
...
This diff is collapsed.
Click to expand it.
net/FlatBuffers/Table.cs
View file @
7c69c5dc
...
...
@@ -125,30 +125,28 @@ namespace FlatBuffers
var
startPos_1
=
offset_1
+
sizeof
(
int
);
var
startPos_2
=
offset_2
+
sizeof
(
int
);
var
len
=
Math
.
Min
(
len_1
,
len_2
);
byte
[]
bbArray
=
bb
.
Data
;
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
if
(
bb
.
Data
[
i
+
startPos_1
]
!=
bb
.
Data
[
i
+
startPos_2
])
return
bb
.
Data
[
i
+
startPos_1
]
-
bb
.
Data
[
i
+
startPos_2
];
if
(
bb
Array
[
i
+
startPos_1
]
!=
bbArray
[
i
+
startPos_2
])
return
bb
Array
[
i
+
startPos_1
]
-
bbArray
[
i
+
startPos_2
];
}
if
(
len_1
<
len_2
)
return
-
1
;
if
(
len_1
>
len_2
)
return
1
;
return
0
;
return
len_1
-
len_2
;
}
// Compare string from the ByteBuffer with the string object
protected
static
int
CompareStrings
(
int
offset_1
,
string
key
,
ByteBuffer
bb
)
protected
static
int
CompareStrings
(
int
offset_1
,
byte
[]
key
,
ByteBuffer
bb
)
{
offset_1
+=
bb
.
GetInt
(
offset_1
);
var
len_1
=
bb
.
GetInt
(
offset_1
);
var
len_2
=
key
.
Length
;
var
startPos_1
=
offset_1
+
sizeof
(
int
);
var
len
=
Math
.
Min
(
len_1
,
len_2
);
byte
[]
bbArray
=
bb
.
Data
;
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
if
(
bb
.
Data
[
i
+
startPos_1
]
!=
key
[
i
])
return
bb
.
Data
[
i
+
startPos_1
]
-
key
[
i
];
if
(
bb
Array
[
i
+
startPos_1
]
!=
key
[
i
])
return
bb
Array
[
i
+
startPos_1
]
-
key
[
i
];
}
if
(
len_1
<
len_2
)
return
-
1
;
if
(
len_1
>
len_2
)
return
1
;
return
0
;
return
len_1
-
len_2
;
}
}
...
...
This diff is collapsed.
Click to expand it.
src/idl_gen_general.cpp
View file @
7c69c5dc
...
...
@@ -699,7 +699,7 @@ std::string GenLookupKeyGetter(flatbuffers::FieldDef *key_field) {
if
(
key_field
->
value
.
type
.
base_type
==
BASE_TYPE_STRING
)
{
key_getter
+=
"comp = "
+
FunctionStart
(
'C'
)
+
"ompareStrings("
;
key_getter
+=
GenOffsetGetter
(
key_field
);
key_getter
+=
",
k
ey, bb);
\n
"
;
key_getter
+=
",
byteK
ey, bb);
\n
"
;
}
else
{
auto
get_val
=
GenGetter
(
key_field
->
value
.
type
)
+
...
...
@@ -1237,6 +1237,11 @@ void GenStruct(StructDef &struct_def, std::string *code_ptr) {
code
+=
"ookupByKey("
+
GenVectorOffsetType
();
code
+=
" vectorOffset, "
+
GenTypeGet
(
key_field
->
value
.
type
);
code
+=
" key, ByteBuffer bb) {
\n
"
;
code
+=
" byte[] byteKey = "
;
if
(
lang_
.
language
==
IDLOptions
::
kJava
)
code
+=
"key.getBytes(StandardCharsets.UTF_8);
\n
"
;
else
code
+=
"System.Text.Encoding.UTF8.GetBytes(key);
\n
"
;
code
+=
" int vectorLocation = "
+
GenByteBufferLength
(
"bb"
);
code
+=
" - vectorOffset.Value;
\n
int span = "
;
code
+=
"bb."
+
FunctionStart
(
'G'
)
+
"etInt(vectorLocation), "
;
...
...
This diff is collapsed.
Click to expand it.
tests/MyGame/Example/Monster.cs
View file @
7c69c5dc
...
...
@@ -136,13 +136,14 @@ public sealed class Monster : Table {
}
public
static
Monster
LookupByKey
(
VectorOffset
vectorOffset
,
string
key
,
ByteBuffer
bb
)
{
byte
[]
byteKey
=
System
.
Text
.
Encoding
.
UTF8
.
GetBytes
(
key
);
int
vectorLocation
=
bb
.
Length
-
vectorOffset
.
Value
;
int
span
=
bb
.
GetInt
(
vectorLocation
),
middle
,
start
=
0
,
comp
,
tableOffset
;
vectorLocation
+=
4
;
while
(
span
!=
0
)
{
int
middle
=
span
/
2
;
tableOffset
=
__indirect
(
vectorLocation
+
4
*
(
start
+
middle
),
bb
);
comp
=
CompareStrings
(
__offset
(
10
,
bb
.
Length
-
tableOffset
,
bb
),
k
ey
,
bb
);
comp
=
CompareStrings
(
__offset
(
10
,
bb
.
Length
-
tableOffset
,
bb
),
byteK
ey
,
bb
);
if
(
comp
>
0
)
span
=
middle
;
else
if
(
comp
<
0
)
{
middle
++;
...
...
This diff is collapsed.
Click to expand it.
tests/MyGame/Example/Monster.java
View file @
7c69c5dc
...
...
@@ -140,13 +140,14 @@ public final class Monster extends Table {
protected
int
keysCompare
(
Integer
o1
,
Integer
o2
,
ByteBuffer
_bb
)
{
return
compareStrings
(
__offset
(
10
,
o1
,
_bb
),
__offset
(
10
,
o2
,
_bb
),
_bb
);
}
public
static
Monster
lookupByKey
(
int
vectorOffset
,
String
key
,
ByteBuffer
bb
)
{
byte
[]
byteKey
=
key
.
getBytes
(
StandardCharsets
.
UTF_8
);
int
vectorLocation
=
bb
.
array
().
length
-
vectorOffset
.
Value
;
int
span
=
bb
.
getInt
(
vectorLocation
),
middle
,
start
=
0
,
comp
,
tableOffset
;
vectorLocation
+=
4
;
while
(
span
!=
0
)
{
int
middle
=
span
/
2
;
tableOffset
=
__indirect
(
vectorLocation
+
4
*
(
start
+
middle
),
bb
);
comp
=
compareStrings
(
__offset
(
10
,
bb
.
array
().
length
-
tableOffset
,
bb
),
k
ey
,
bb
);
comp
=
compareStrings
(
__offset
(
10
,
bb
.
array
().
length
-
tableOffset
,
bb
),
byteK
ey
,
bb
);
if
(
comp
>
0
)
span
=
middle
;
else
if
(
comp
<
0
)
{
middle
++;
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment