Skip to content

Commit

Permalink
Handle pointers larger than Long.MAX_VALUE
Browse files Browse the repository at this point in the history
Closes #562
  • Loading branch information
oakkitten committed Feb 26, 2023
1 parent 66998a3 commit 2fdf0a0
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class NicklistDialog : ListDialog(),
this.buffer = buffer
this.title = buffer.shortName
this.adapter = NicklistAdapter(requireContext()) {
Events.SendMessageEvent.fire("input 0x%x /query -noswitch %s", buffer.pointer, it.name)
Events.SendMessageEvent.fireInput(buffer, "/query -noswitch ${it.name}")
dismiss()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ class Buffer @WorkerThread constructor(
@MainThread @Synchronized fun moveReadMarkerToEnd() {
lines.moveReadMarkerToEnd()
if (P.hotlistSync) Events.SendMessageEvent.fire(
"input 0x%1${"$"}x /buffer set hotlist -1\n" +
"input 0x%1${"$"}x /input set_unread_current_buffer", pointer)
"input ${pointer.as0x} /buffer set hotlist -1\n" +
"input ${pointer.as0x} /input set_unread_current_buffer")
}

val hotCount: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.ubergeek42.WeechatAndroid.utils.SHOULD_EMOJIFY
import com.ubergeek42.WeechatAndroid.utils.emojify
import com.ubergeek42.weechat.Color
import com.ubergeek42.weechat.ColorScheme
import java.lang.Long.toHexString


open class Line constructor(
Expand Down Expand Up @@ -104,5 +103,5 @@ open class Line constructor(
val timestampedIrcLikeString: String get() =
timestampString?.let { timestamp -> "$timestamp $ircLikeString" } ?: ircLikeString

override fun toString() = "Line(0x${toHexString(pointer)}: $ircLikeString)"
override fun toString() = "Line(${pointer.as0x}): $ircLikeString)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ inline fun Hdata.forEachExistingBuffer(block: (spec: BufferSpec, buffer: Buffer)
}


val Long.as0x get() = "0x" + java.lang.Long.toHexString(this)
val Long.as0x get() = "0x" + java.lang.Long.toUnsignedString(this, 16)

val String.from0x: Long get() = java.lang.Long.decode(this)
val String.from0x: Long get() = java.lang.Long.parseUnsignedLong(this.substring(2), 16)

val String.from0xOrNull get() = try { from0x } catch (e: NumberFormatException) { null }
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ class Events {
EventBus.getDefault().post(SendMessageEvent(message))
}

fun fire(message: String, vararg args: Any) {
fire(String.format(Locale.ROOT, message, *args))
}

fun fireInput(buffer: Buffer, input: String?) {
if (input.isNullOrEmpty()) return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.widget.EditText
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.coroutineScope
import com.ubergeek42.WeechatAndroid.relay.Buffer
import com.ubergeek42.WeechatAndroid.relay.as0x
import com.ubergeek42.WeechatAndroid.upload.suppress
import com.ubergeek42.weechat.relay.protocol.Hdata
import com.ubergeek42.weechat.relay.protocol.RelayObject
Expand Down Expand Up @@ -45,7 +46,7 @@ class OnlineTabCompleter(
// so instead we rely on base word, assuming that it ends on selection end
private suspend fun fetchCompletions(): Iterator<Replacement> {
val selectionStart = input.selectionStart
val message = "completion 0x%x %d %s".format(buffer.pointer, selectionStart, input.text)
val message = "completion ${buffer.pointer.as0x} $selectionStart ${input.text}"

val (completions, baseWord, addSpace) = queryWeechat(message).asCompletions()
?: return EmptyReplacements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ public interface Predicate<T> {
}

public static String pointerToString(long pointer) {
return Long.toHexString(pointer);
return Long.toUnsignedString(pointer, 16);
}

public static long pointerFromString(String strPointer) {
try {
return Long.parseLong(strPointer, 16);
return Long.parseUnsignedLong(strPointer, 16);
} catch (NumberFormatException ignored) {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public char getChar() {
return (char) getByte();
}

// Might have to change to a BigInteger...
// What we are given directly corresponds to a regular signed long.
// https://weechat.org/files/doc/stable/weechat_relay_protocol.en.html#object_long_integer
public long getLongInteger() {
int length = getByte();
if (pointer + length > data.length) {
Expand Down Expand Up @@ -129,17 +130,13 @@ public String getPointer() {
throw new IndexOutOfBoundsException("Not enough data");
}

StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder("0x");

for (int i = 0; i < length; i++) {
sb.append(getChar());
}

if (length == 1 && Long.parseLong(sb.toString().toUpperCase(Locale.ENGLISH), 16) == 0) {
// Null Pointer
return "0x0";
}

return "0x" + sb.toString();
return sb.toString();
}

// Maybe return a reasonable "Date" object or similar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
*
*/
public class HdataEntry extends RelayObject {
private ArrayList<String> pointers = new ArrayList<String>();
private HashMap<String, RelayObject> data = new HashMap<String, RelayObject>();
private final ArrayList<String> pointers = new ArrayList<String>();
private final HashMap<String, RelayObject> data = new HashMap<String, RelayObject>();

protected void addPointer(String pointer) {
pointers.add(pointer);
Expand Down Expand Up @@ -83,7 +83,7 @@ public String getPointer() {

public long getPointerLong() {
try {
return Long.parseLong(getPointer().substring(2), 16);
return Long.parseUnsignedLong(getPointer().substring(2), 16);
} catch (Exception e) {
return -1;
}
Expand All @@ -102,7 +102,7 @@ public String getPointer(int index) {

public long getPointerLong(int index) {
try {
return Long.parseLong(getPointer(index).substring(2), 16);
return Long.parseUnsignedLong(getPointer(index).substring(2), 16);
} catch (Exception e) {
return -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public String asPointer() {

public long asPointerLong() {
try {
return Long.parseLong(asPointer().substring(2), 16);
return Long.parseUnsignedLong(asPointer().substring(2), 16);
} catch (Exception e) {
return -1;
}
Expand Down

0 comments on commit 2fdf0a0

Please sign in to comment.