Skip to content

Commit

Permalink
[21_7] Use string_view<char> to re-impl ==, !=, <, <= of string
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii authored Mar 29, 2024
1 parent c242861 commit c21ce73
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
28 changes: 4 additions & 24 deletions Kernel/Types/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,12 @@ string::operator!= (const char* s) {

bool
string::operator== (string a) {
int i;
if (rep->n != a->n) return false;
char *S_left= rep->a, *S_right= a->a;
for (i= 0; i < rep->n; i++)
if (S_left[i] != S_right[i]) return false;
return true;
return ((string_view<char>) *this) == ((string_view<char>) a);
}

bool
string::operator!= (string a) {
int i;
if (rep->n != a->n) return true;
char *S_left= rep->a, *S_right= a->a;
for (i= 0; i < rep->n; i++)
if (S_left[i] != S_right[i]) return true;
return false;
return ((string_view<char>) *this) != ((string_view<char>) a);
}

string
Expand Down Expand Up @@ -185,22 +175,12 @@ operator* (string a, const char* b) {

bool
operator< (string s1, string s2) {
int i, n1= N (s1), n2= N (s2), nmin= min (n1, n2);
for (i= 0; i < nmin; i++) {
if (s1[i] < s2[i]) return true;
if (s2[i] < s1[i]) return false;
}
return n1 < n2;
return ((string_view<char>) s1) < ((string_view<char>) s2);
}

bool
operator<= (string s1, string s2) {
int i, n1= N (s1), n2= N (s2), nmin= min (n1, n2);
for (i= 0; i < nmin; i++) {
if (s1[i] < s2[i]) return true;
if (s2[i] < s1[i]) return false;
}
return n1 <= n2;
return ((string_view<char>) s1) <= ((string_view<char>) s2);
}

int
Expand Down
7 changes: 7 additions & 0 deletions Kernel/Types/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
#define STRING_H
#include "classdef.hpp"
#include "fast_alloc.hpp"
#include "lolly/data/string_view.hpp"
#include "minmax.hpp"
#include <stdint.h>

using lolly::data::string_view;

class string;
class string_rep : concrete_struct {
int n;
Expand Down Expand Up @@ -48,6 +51,10 @@ class string {
bool operator== (string s);
bool operator!= (string s);
string operator() (int start, int end);

inline operator string_view<char> () {
return string_view<char> (rep->a, rep->n);
}
};
CONCRETE_CODE (string);

Expand Down

0 comments on commit c21ce73

Please sign in to comment.