Skip to content

Instantly share code, notes, and snippets.

@AliWisam
Created March 25, 2022 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AliWisam/6e91ab55c2a1adc28454746408c250d3 to your computer and use it in GitHub Desktop.
Save AliWisam/6e91ab55c2a1adc28454746408c250d3 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=true&runs=200&gist=
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
// https://github.com/HermesAteneo/
import "https://github.com/Arachnid/solidity-stringutils/blob/master/src/strings.sol";
contract RepeatedWords {
//using library
using strings for *;
// //for returning first word in string
// function smt(string memory _str) public pure returns(string memory){
// strings.slice memory s = _str.toSlice();
// strings.slice memory delim = "".toSlice();
// string[] memory parts = new string[](s.count(delim));
// for (uint i = 0; i < parts.length; i++) {
// parts[i] = s.split(delim).toString();
// //toDo
// //make an inner loop
// }
// return parts[0];
// }
// string what = "s";
// where = solidity
////////////////
modifier contains (string memory what, string memory where) {
//converting to bytes array
bytes memory whatBytes = bytes (what);
bytes memory whereBytes = bytes (where);
require(whereBytes.length >= whatBytes.length,"lenght of what shoule be greater than where");
bool found = false;
for (uint i = 0; i <= whereBytes.length - whatBytes.length; i++) {
bool flag = true;
for (uint j = 0; j < whatBytes.length; j++)
if (whereBytes [i + j] != whatBytes [j]) {
flag = false;
break;
}
if (flag) {
found = true;
break;
}
}
require (found,"not found" );
_;
}
// it will return true if str contains 's'
function check(string memory str) public pure contains ("s", str) returns(bool,string memory) {
bytes memory a = new bytes(1);
a[0] = bytes(str)[0];
return (true,string(a));
}
// returns number of repeated characters
// main function
function HowManyRepeated(string memory what, string memory where) public pure returns(uint, string memory){
uint times = 0;
if( ContainWord( what, where ) ){
uint whatLen = CountUTF8String(what);
uint whereLen = CountUTF8String(where);
for (uint i = 0; i < whereLen - whatLen + 1 ; i++) {
if( ContainWord( what, Substring( where, i , i + whatLen) ) ){
times++;
}
}
}
bytes memory a = new bytes(1);
a[0] = bytes(what)[0];
return (times,string(a));
}
//counting number of characters
function CountUTF8String(string memory str) internal pure returns (uint256 length){
uint i=0;
bytes memory string_rep = bytes(str);
// >> is shift operator
while (i<string_rep.length){
if (string_rep[i]>>7==0)
i+=1;
else if (string_rep[i]>>5==bytes1(uint8(0x6)))
i+=2;
else if (string_rep[i]>>4==bytes1(uint8(0xE)))
i+=3;
else if (string_rep[i]>>3==bytes1(uint8(0x1E)))
i+=4;
else
//For safety
i+=1;
length++;
}
}
// same like contains modifier
function ContainWord (string memory what, string memory where) internal pure returns (bool found){
bytes memory whatBytes = bytes (what);
bytes memory whereBytes = bytes (where);
//require(whereBytes.length >= whatBytes.length);
if(whereBytes.length < whatBytes.length){ return false; }
found = false;
for (uint i = 0; i <= whereBytes.length - whatBytes.length; i++) {
bool flag = true;
for (uint j = 0; j < whatBytes.length; j++)
if (whereBytes [i + j] != whatBytes [j]) {
flag = false;
break;
}
if (flag) {
found = true;
break;
}
}
return found;
}
function Substring(string memory str, uint startIndex, uint endIndex) internal pure returns (string memory ) {
bytes memory strBytes = bytes(str);
bytes memory result = new bytes(endIndex-startIndex);
for(uint i = startIndex; i < endIndex; i++) {
result[i-startIndex] = strBytes[i];
}
return string(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment