commit fd701678aed478e2034f29fd890f6591b35a4af1
parent b5da7c6c76348ff0c4097b9ef72da3cfdaa1270b
Author: Andrew Laack <andrew@laack.co>
Date: Tue, 22 Sep 2026 14:53:14 -0500
Cache prior status codes for most recent visit to optimize back / forward
Diffstat:
2 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/include/browser.hpp b/include/browser.hpp
@@ -1,6 +1,7 @@
#pragma once
#include <cstddef>
+#include <unordered_map>
#include <vector>
#include "line.hpp"
#include "site.hpp"
@@ -8,6 +9,7 @@
class Browser {
private:
+ std::unordered_map<std::string, int> previousStatusCodes;
std::vector<Link*> siteHistory;
int previousIdx = -1;
Site* currentSite;
diff --git a/src/browser.cpp b/src/browser.cpp
@@ -49,6 +49,7 @@ void Browser::goToSite(std::string url, bool addToHistory) {
currentSite = site;
lines = toLines(site);
setLinksOfCurrentLines();
+ previousStatusCodes[destination->getLinkDestination().to_string()] = site->getStatusCode();
}
void Browser::setLinksOfCurrentLines() {
@@ -114,18 +115,23 @@ void Browser::followLinkNumber(int linkToFollow) {
void Browser::goBack() {
previousIdx -= 1;
- goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false);
- while(!(this->getCurrentSite()->getStatusCode() >= 20 && this->getCurrentSite()->getStatusCode() <= 29)) {
+ int prSC = previousStatusCodes[siteHistory[previousIdx]->getLinkDestination().to_string()];
+ while(!(prSC >= 20 && prSC <= 29)) {
previousIdx -= 1;
- goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false);
+ prSC = previousStatusCodes[siteHistory[previousIdx]->getLinkDestination().to_string()];
}
+
+ goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false);
}
void Browser::goForward() {
previousIdx += 1;
- goToSite(siteHistory[previousIdx]->getLinkDestination().to_string(), false);
- while(!(this->getCurrentSite()->getStatusCode() >= 20 && this->getCurrentSite()->getStatusCode() <= 29)) {
+
+ int prSC = previousStatusCodes[siteHistory[previousIdx]->getLinkDestination().to_string()];
+ while(!(prSC >= 20 && prSC <= 29)) {
previousIdx += 1;
- goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false);
+ prSC = previousStatusCodes[siteHistory[previousIdx]->getLinkDestination().to_string()];
}
+
+ goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false);
}