Created
March 25, 2022 12:54
-
-
Save AliWisam/de3bf9d2bfe2c47bce3e1728d576b807 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.3+commit.8d00100c.js&optimize=true&runs=400&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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) { | |
//todo | |
//call smt function | |
return (true); | |
} | |
// returns number of repeated characters | |
// main function | |
function HowManyRepeated(string memory what, string memory where) public pure returns(uint){ | |
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++; | |
} | |
} | |
} | |
//todo | |
//call smt function | |
return times; | |
} | |
//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