Skip to content

Commit

Permalink
Merge branch 'bad-buffer-ids' into v1.9-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
oakkitten committed Jan 28, 2024
2 parents 04f7996 + 2fdf0a0 commit 1517beb
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 103 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 }

This file was deleted.

36 changes: 36 additions & 0 deletions app/src/main/java/com/ubergeek42/WeechatAndroid/service/Events.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.

package com.ubergeek42.WeechatAndroid.service

import com.ubergeek42.WeechatAndroid.relay.Buffer
import com.ubergeek42.WeechatAndroid.relay.as0x
import com.ubergeek42.WeechatAndroid.utils.Assert
import org.greenrobot.eventbus.EventBus
import java.util.EnumSet
import java.util.Locale

class Events {
data class StateChangedEvent(@JvmField val state: EnumSet<RelayService.STATE>)

data class ExceptionEvent(@JvmField val e: Exception)

data class SendMessageEvent(@JvmField val message: String) {
companion object {
fun fire(message: String) {
Assert.assertThat(message.endsWith("\n")).isFalse()
EventBus.getDefault().post(SendMessageEvent(message))
}

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

P.addSentMessage(input)

input.lineSequence().filter(String::isNotEmpty).forEach { line ->
fire("input ${buffer.pointer.as0x} $line")
}
}
}
}
}
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 1517beb

Please sign in to comment.